﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using System.Data.SqlClient;
using System.IO;

namespace PlanningDepartment
{
    public partial class FrCungTieu : Telerik.WinControls.UI.RadForm
    {
        public FrCungTieu()
        {
            InitializeComponent();
        }
        #region //Class Public
        private void ChangeThemeName(Control control, string themeName)
        {
            IComponentTreeHandler radControl = control as IComponentTreeHandler;
            if (radControl != null) { radControl.ThemeName = themeName; }
            foreach (Control child in control.Controls)
            { ChangeThemeName(child, themeName); }
        }
        private SqlConnection Conn;
        private Process prs = new Process();

        private DataSet ds = new DataSet();
        private string string_temp = null, child = " - ", DVD1,DVD2,DVD3,DVD1T,DVD2T,DVD3T,Wait1, Vanh1, Datebegin, Dateend, GhiChu1, PhanLoai1, SoLuong1, SoKhoi1, DVT1, KhachHang1, NoiGiao1, NoiNhan1, ADorKD1, BienBan1, ChungTu1, SoXe1, Ngay1,LyDo1,LoaiGo1,Serial1,SoKhoi1T,ID1,Serial2,TenSP1,MaSP1,DonGia1,ThanhTien1,Nhom1;
        private int i = 0, j = 0, k = 0, count = 0, int_temp = 0;
        private double temp_double = 0, SLNhap_d, SLXuat_d, SoKhoi_d, SoLuong_d,DonGia_d=0,ThanhTien_d=0;
        //private DateTime time;
        private void Mni_Office2010Black_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2010Black.Text);
        }
        private void Mni_Office2010Blue_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2010Blue.Text);
        }

        private void Mni_TelerikMetroBlue_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_telerikMetroBlue.Text);
        }

        private void Mni_Office2007Black_Click(object sender, EventArgs e)
        {
            ChangeThemeName(this, Mni_Office2007Black.Text);
        }
       
        private void cb1(RadDropDownList cb, string sql, SqlConnection Conn, int column1, int column2)
        {
            //Data dta = new Data();
            //SqlConnection Conn = dta.Connect();
            //Conn = prs.Connect_DBScansia();
            try
            {

                SqlCommand cmd = new SqlCommand(sql, Conn);
                SqlDataReader rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    cb.Items.Add(rd[column1].ToString() + " - " + rd[column2].ToString());
                }

                cmd.Dispose();
                rd.Close();
            }
            catch (Exception ex)
            { MessageBox.Show("Lỗi load Combobox " + ex.Message); }

        }
        
       
        private string Date_Split(string Date)
        {
            string Ngay_split;
            string[] temp = Date.Split('/');
            Ngay_split = temp[1] + "/" + temp[0] + "/" + temp[2];
            return Ngay_split;
        }
        private void RowFormatting(RadGridView gr, object sender, RowFormattingEventArgs e)
        {

            //radGridView2.Rows.



        }
       
        /*private void Mni_DangNhap_Click(object sender, EventArgs e)
        {
            txt_UserName.Visible = true;
            txt_Password.Visible = true;
            bt_DangNhap.Visible = true;
            lb_DangNhap.Visible = true;
        }

        private void Mni_DangXuat_Click(object sender, EventArgs e)
        {
            Page_Main.Visible = false;
            txt_UserName.Visible = true;
            txt_Password.Visible = true;
            lb_DangNhap.Visible = true;
            txt_UserName.Focus();
        }*/
        public void Users(string Users)
        {
            Process dta = new Process();
            string sql = @"SELECT
                      [UsersName]
                      ,[Password]
                      ,[GroupUser]
                      ,[Area]
                      ,[Images]
                      ,[MaNV]
                      ,[ChucVu]
                      ,[HovaTen]
                      ,convert(varchar(20),[DateAccess],113) as DateAccess
      
                  FROM [dbo].[Users] where UsersName='" + Users + "'";
            //if (dta.check_input(sql, txt_UserName.Text, 0) && dta.check_input(sql, txt_Password.Text, 1))
            SqlConnection Conn = dta.Connect();
            SqlCommand cmd = new SqlCommand();
            try
            {
                Conn.Open();
                cmd.Connection = Conn;
                cmd.CommandText = sql;
                SqlDataReader rd = cmd.ExecuteReader();

                while (rd.Read())
                {

                    Enable_form(rd["HovaTen"].ToString(), rd["Area"].ToString());
                    lb_Useraccount.Text = rd["UsersName"].ToString();
                    lb_HovaTen.Text = rd["HovaTen"].ToString();
                    lb_ChucVu.Text = rd["ChucVu"].ToString();
                    lb_MaNV.Text = rd["MaNV"].ToString();
                    lb_NgayTruyCap.Text = rd["DateAccess"].ToString();
                   
                    if (DBNull.Value.Equals(rd["Images"]))
                    {
                        //MessageBox.Show("aaa");
                        pictureBox1.Image = null;
                    }
                    else
                    {
                        dta.get_image(pictureBox1, (byte[])(rd["Images"]));
                    }
                    //MessageBox.Show("asasdas");

                }
                rd.Close();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        byte[] ReadFile(string sPath)
        {
            //Initialize byte array with a null value initially.
            byte[] data = null;

            //Use FileInfo object to get file size.
            FileInfo fInfo = new FileInfo(sPath);
            long numBytes = fInfo.Length;

            //Open FileStream to read file
            FileStream fStream = new FileStream(sPath, FileMode.Open, FileAccess.Read);

            //Use BinaryReader to read file stream into byte array.
            BinaryReader br = new BinaryReader(fStream);

            //When you use BinaryReader, you need to supply number of bytes to read from file.
            //In this case we want to read entire file. So supplying total number of bytes.
            data = br.ReadBytes((int)numBytes);
            return data;
        }
        private void Enable_form(string Username, string groupuser)
        {
            Page_Main.Visible = true;
            Page_User.Text = Username;
        }
        private void Load_()
        {
            SqlCommand cmd = new SqlCommand();
            Conn = prs.Connect();
            Conn.Open();
            try
            {
                string[] arr_LoaiGo = new string[] { "Malaysia Acacia", "Laos Acacia", "Laos eu" };
                for (i = 0; i < arr_LoaiGo.Length; i++)
                {

                    cb_LoaiGo_BCKG.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_BCLS.Items.Add(arr_LoaiGo[i].ToString());
                    cb_LoaiGo_BKKG.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_CTKG.Items.Add(arr_LoaiGo[i].ToString());
                    cb_LoaiGo_CTLS.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_inputKG.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_inputLS.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_outputKG.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_outputLS.Items.Add(arr_LoaiGo[i].ToString());
                    cb_NguonGo_TranKG.Items.Add(arr_LoaiGo[i].ToString());
                }
                cmd.CommandText = "select MaNGN,TenNGN from dbo.NoiGiaoNhan";
                cmd.Connection = Conn;
                
                SqlDataReader rd = cmd.ExecuteReader();
               
                while (rd.Read())
                {
                    
                    cb_NoiGiao_inputKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_inputKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_outputKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_outputKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_CTKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_CTKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_BCKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_BCKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_BKKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_BKKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_inputLS.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_inputLS.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_CTLS.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_CTLS.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_outputLS.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_inputNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_inputNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_outputNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_outputNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiNhan_NVLCT.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_NoiGiao_NVLCT.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_LoaiGo_BCKG.Items.Add(rd[0].ToString());
                    cb_NguonGo_BCLS.Items.Add(rd[0].ToString());
                    cb_LoaiGo_BKKG.Items.Add(rd[0].ToString());
                    cb_NguonGo_CTKG.Items.Add(rd[0].ToString());
                    cb_LoaiGo_CTLS.Items.Add(rd[0].ToString());
                    cb_NguonGo_inputKG.Items.Add(rd[0].ToString());
                    cb_NguonGo_inputLS.Items.Add(rd[0].ToString());
                    cb_NguonGo_outputKG.Items.Add(rd[0].ToString());
                    cb_NguonGo_outputLS.Items.Add(rd[0].ToString());
                    cb_NguonGo_TranKG.Items.Add(rd[0].ToString());
                }
                rd.Close();
                cmd.CommandText = "select MaLyDo,LyDo from dbo.LyDo where KV='CO1'";
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    cb_LyDo_inputKG.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_outputKG.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_CTKG.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    //cb_LyDo_KV5Search.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_BCKG.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_BKKG.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_inputNVL.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_outputNVL.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    cb_LyDo_NVLCT.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                    //cb_LyDo_CTLS.Items.Add(rd[1].ToString() + " - " + rd[0].ToString());
                }
                rd.Close();
                cmd.CommandText="SELECT [QuyCach],CAST(CDay as varchar)+'x'+CAST(CRong as varchar)+'x'+CAST(CDai as varchar) as size,CDay,CRong,CDai,PhanLoai FROM [Planning].[dbo].[QuyCach]";
                rd = cmd.ExecuteReader();
                 while (rd.Read())
                {
                    cb_QuyCach_inputKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_QuyCach_outputKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_QuyCach_TranKG.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_QuyCach_TranKG1.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    cb_QuyCach_inputLS.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                    dataGridView_QuyCach.Rows.Add(rd["QuyCach"].ToString(),rd["size"].ToString(), rd["CDay"].ToString(), rd["CRong"].ToString(), rd["CDai"].ToString(),rd["PhanLoai"]);
                    

                    
                }
                 rd.Close();
                 cmd.CommandText = "SELECT [MaHang],[TenHang] FROM [dbo].[NVL_VatTu]";
                  rd = cmd.ExecuteReader();
                  while (rd.Read())
                  {
                      cb_TenSP_inputNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                      cb_TenSP_outputNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                      cb_TenSP_TKNVL.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                      cb_TenSP_NVLRP.Items.Add(rd[0].ToString() + " - " + rd[1].ToString());
                  }
                 
                  string[] arr_PhanLoai = new string[] { "Sơ chế","BSSC","Phách" };
                  for (i = 0; i < arr_PhanLoai.Length; i++)
                  {
                      cb_PhanLoai_QC.Items.Add(arr_PhanLoai[i].ToString());
                      cb_PhanLoai_TranKG.Items.Add(arr_PhanLoai[i].ToString());
                      cb_PhanLoai_inputKG.Items.Add(arr_PhanLoai[i].ToString());
                      cb_PhanLoai_inputLS.Items.Add(arr_PhanLoai[i].ToString());
                      cb_PhanLoai_outputKG.Items.Add(arr_PhanLoai[i].ToString());
                     
                  }
                rd.Dispose();
                //DataSet ds = new DataSet();


                //string_temp = @"select dbo.Load_CTSP_Mua.MaSP,dbo.Load_CTSP_Mua.TenChiTiet+' - '+dbo.Load_CTSP_Mua.MaChiTiet from dbo.Load_CTSP_Mua inner join dbo.ChiTiet_KhuVuc on ChiTiet_KhuVuc.MaCT=dbo.Load_CTSP_Mua.MaChiTiet and (ChiTiet_KhuVuc.KhuVuc='KV2XP' or ChiTiet_KhuVuc.KhuVuc='KV3')";
               // ds = prs.dts(string_temp, Conn, ds);
               

                ChangeThemeName(this, "Office2010Blue");
                Control control = this;
                IComponentTreeHandler radControl = control as IComponentTreeHandler;
                if (radControl != null) { radControl.ThemeName = "Office2010Blue"; }
                cmd.CommandText = "select convert(varchar(10),getdate(),103)";
                string_temp = cmd.ExecuteScalar().ToString();
                dtp_Date_CTKG.Text = string_temp;
                dtp_Date_inputKG.Text = string_temp;
                dtp_Date_outputKG.Text = string_temp;
                dtp_Date_outputLS.Text = string_temp;
                dtp_Date_TranKG.Text = string_temp;
                dtp_Datebegin_BCKG.Text = string_temp;
                dtp_Datebegin_BKKG.Text = string_temp;
                dtp_Datebegin_BKLS.Text = string_temp;
                dtp_DateBegin_CTLS.Text = string_temp;
                dtp_Datebegin_inputLS.Text = string_temp;
                dtp_Datebegin_TKKG.Text = string_temp;
                dtp_DateBeginRL_BCLS.Text = string_temp;
                dtp_DateBeginVL_BCLS.Text = string_temp;
                dtp_Dateend_BCKG.Text = string_temp;
                dtp_Dateend_BKKG.Text = string_temp;
                dtp_Dateend_BKLS.Text = string_temp;
                dtp_DateEnd_CTLS.Text = string_temp;
                dtp_Dateend_inputLS.Text = string_temp;
                dtp_Dateend_TKKG.Text = string_temp;
                dtp_DateEndRL_BCLS.Text = string_temp;
                dtp_DateEndVL_BCLS.Text = string_temp;
                dtp_Date_inputNVL.Text = string_temp;
                dtp_Date_outputNVL.Text = string_temp;
                dtp_DateBegin_NVLRP.Text = string_temp;
                dtp_DateEnd_NVLRP.Text = string_temp;
                dtp_Date_NVLCT.Text = string_temp;
                dtp_Date_NVLBK.Text = string_temp;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
                cmd.Dispose();
            }
        }
        private void Page_Main_SelectedPageChanged(object sender, EventArgs e)
        {
            if (Page_Main.SelectedPage == Page_NVL)
            {
                string_temp = @"SELECT NVL_QuyDoi.MaSP,NVL_VatTu.TenHang,isnull(DonGia,0) as DonGia,DVT,NVL_QuyDoi.Nhom,NVL_QuyDoi.SLQuyDoi,NVL_QuyDoi.MaChiTiet
                                FROM [dbo].[NVL_VatTu] inner join dbo.NVL_QuyDoi on NVL_VatTu.MaHang=NVL_QuyDoi.MaChiTiet";
                Conn = prs.Connect();
                Conn.Open();
                DataSet ds = new DataSet(); 
                ds=prs.dts(string_temp, Conn, ds);
                dataGridView1.DataSource = ds.Tables[0];
               // MessageBox.Show("aaa");
            }
        }
        private void FrCungTieu_KeyDown(object sender, KeyEventArgs ex)
        {
            if (ex.KeyData == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
            }
            if (ex.KeyData == Keys.F1)
            {
                if (Page_Main.SelectedPage == Page_KhoGo)
                {
                    if (Page_Xuat_KG.SelectedPage == Page_NhapGo)
                    {
                        print_TH_inputKG();
                    }

                    if (Page_Xuat_KG.SelectedPage == Page_Xuat_KGo)
                        print_TH_outputKG();
                   
                }
                if (Page_Main.SelectedPage == Page_LoSay)
                {
                    if (Page_Main_LoSay.SelectedPage == Page_LoSayinput)
                        print_TH_inputLS();
                    if (Page_Main_LoSay.SelectedPage == Page_KienRaLo)
                        print_TH_outputLS();
                }
                if (Page_Main.SelectedPage == Page_NVL)
                {
                    if (Page_Other.SelectedPage == Page_input_Other)
                    {
                        print_inputNVL_TH();
                    }
                    if (Page_Other.SelectedPage == Page_output_Other)
                    {
                        print_outputNVL_TH();
                    }
                }
               
            }
            if (ex.KeyData == Keys.F2)
            {
                if (Page_Main.SelectedPage == Page_KhoGo)
                {
                    if (Page_Xuat_KG.SelectedPage == Page_NhapGo)
                    {
                        print_inputKG();
                    }

                    if (Page_Xuat_KG.SelectedPage == Page_Xuat_KGo)
                        print_outputKG();
                }
                if (Page_Main.SelectedPage == Page_LoSay)
                {
                    if (Page_Main_LoSay.SelectedPage == Page_LoSayinput)
                        print_inputLS();
                }
            }
            if (ex.KeyData == Keys.F5)
            {
                if (Page_Main.SelectedPage == Page_LoSay)
                {
                    if (Page_Main_LoSay.SelectedPage == Page_ReportLS)
                        bt_Search_BCLS_Click(null, null);
                }
            }
            if (ex.KeyData == Keys.PageDown)
            {
                if (Page_Main.SelectedPage == Page_KhoGo)
                {
                    if (Page_Xuat_KG.SelectedPage == Page_NhapGo)
                    {
                        bt_add_inputKG_Click(null, null);
                    }

                    if (Page_Xuat_KG.SelectedPage == Page_Xuat_KGo)
                        bt_add_outputKG_Click(null, null);
                }
                if (Page_Main.SelectedPage == Page_LoSay)
                {
                    if (Page_Main_LoSay.SelectedPage == Page_LoSayinput)
                        bt_add_inputLS_Click(null, null);
                }
            }
            if (ex.KeyData == Keys.F11)
            {
                if (Page_Main.SelectedPage == Page_KhoGo)
                {
                    if (Page_Xuat_KG.SelectedPage == Page_NhapGo)
                    {
                        if (bt_save_inputKG.Enabled == true)
                            bt_save_inputKG_Click(null, null);
                        else
                            MessageBox.Show("Bạn đang làm không đúng");
                    }

                    if (Page_Xuat_KG.SelectedPage == Page_Xuat_KGo)
                    {
                        if (bt_save_outputKG.Enabled == true)
                        {
                            bt_save_outputKG_Click(null, null);
                        }
                        else
                            MessageBox.Show("Bạn đang làm không đúng");
                    }
                }
                if (Page_Main.SelectedPage == Page_LoSay)
                {
                    if (Page_Main_LoSay.SelectedPage == Page_LoSayinput)
                    {
                        if (bt_save_inputLS.Enabled == true)
                        {
                            bt_save_inputLS_Click(null, null);
                        }
                        else
                            MessageBox.Show("Bạn đang làm không đúng");

                    }
                    
                }
            }
        }
         private void FrCungTieu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control)
            {
                if (e.KeyCode.Equals(Keys.W))
                {
                    MessageBox.Show("Bạn đã kích hoạt chức năng chờ xử lý, lô hàng này sẽ nằm trong danh sách chờ xử lý");
                     chk_ChoXuLy.Enabled = true;
                   
                }
                if (e.KeyCode.Equals(Keys.N))
                {
                    Conn = prs.Connect();
                    SqlCommand cmd = new SqlCommand();
                    Conn.Open();
                    cmd.Connection = Conn;

                    //cmd.CommandText = sql;
                    SqlDataReader rd = null;
                    try
                    {
                        // while (rd.Read())
                        if (Page_Main.SelectedPage == Page_KhoGo)
                        {

                            if (Page_Xuat_KG.SelectedPage == Page_NhapGo)
                            {
                                if (txt_Serial_input.Text == "")
                                    MessageBox.Show("Bạn cần nhập số Serial để kích hoạt chức năng này");
                                else
                                {
                                    string_temp = "select Nhom from dbo.NhapGo where Serial=" + txt_Serial_input.Text;
                                    cmd.CommandText = string_temp;
                                    rd = cmd.ExecuteReader();
                                    while (rd.Read())
                                    {
                                        if (rd[0].ToString() == "Phach")
                                        {
                                            rd_Phach_inputKG.Enabled = true;
                                            rd_Phach_inputKG.IsChecked = true;
                                            rd_Van_inputKG.Enabled = false;
                                            rd_GoTron_inputKG.Enabled = false;

                                        }
                                        if (rd[0].ToString() == "Van")
                                        {
                                            rd_Van_inputKG.Enabled = true;
                                            rd_Phach_inputKG.Enabled = false;
                                            rd_Van_inputKG.IsChecked = true;
                                            rd_GoTron_inputKG.Enabled = false;
                                        }
                                        if (rd[0].ToString() == "GoTron")
                                        {
                                            rd_GoTron_inputKG.Enabled = true;
                                            rd_Phach_inputKG.Enabled = false;
                                            rd_Van_inputKG.Enabled = false;
                                            rd_GoTron_inputKG.IsChecked = true;
                                        }
                                        break;
                                    }
                                    MessageBox.Show("Bạn đã kích hoạt chức năng cập nhật thêm hàng hóa vào chứng từ: " + txt_Serial_input.Text);
                                    lb_CapNhat_input.Visible = true;
                                    lb_Serial_addinput.Visible = true;
                                    lb_Serial_addinput.Text = txt_Serial_input.Text;
                                    if (radGridView1.Rows.Count > 0)
                                        radGridView1.Rows.Clear();

                                }
                            }
                            if (Page_Xuat_KG.SelectedPage == Page_Xuat_KGo)
                            {
                                if (txt_Serial_output.Text == "")
                                    MessageBox.Show("Bạn cần nhập số Serial để kích hoạt chức năng này");
                                else
                                {
                                    string_temp = "select Nhom from dbo.NhapGo where Serial=" + txt_Serial_output.Text;
                                    cmd.CommandText = string_temp;
                                    rd = cmd.ExecuteReader();
                                    while (rd.Read())
                                    {
                                        if (rd[0].ToString() == "Phach")
                                        {
                                            rd_Phach_outputKG.Enabled = true;
                                            rd_Phach_outputKG.IsChecked = true;
                                            rd_Van_outputKG.Enabled = false;
                                            rd_GoTron_outputKG.Enabled = false;

                                        }
                                        if (rd[0].ToString() == "Van")
                                        {
                                            rd_Van_outputKG.Enabled = true;
                                            rd_Phach_outputKG.Enabled = false;
                                            rd_Van_outputKG.IsChecked = true;
                                            rd_GoTron_outputKG.Enabled = false;
                                        }
                                        if (rd[0].ToString() == "GoTron")
                                        {
                                            rd_GoTron_outputKG.Enabled = true;
                                            rd_Phach_outputKG.Enabled = false;
                                            rd_Van_outputKG.Enabled = false;
                                            rd_GoTron_outputKG.IsChecked = true;
                                        }
                                        break;

                                    }
                                    MessageBox.Show("Bạn đã kích hoạt chức năng cập nhật thêm hàng hóa vào chứng từ");
                                    lb_CapNhat_output.Visible = true;
                                    lb_Serial_addoutput.Visible = true;
                                    lb_Serial_addoutput.Text = txt_Serial_output.Text;
                                    if (radGridView2.Rows.Count > 0)
                                        radGridView2.Rows.Clear();
                                }
                            }

                        }
                        if (Page_Main.SelectedPage == Page_LoSay)
                        {
                            if (Page_Main_LoSay.SelectedPage == Page_LoSayinput)
                            {
                                if (txt_Serial_inputLS.Text == "")
                                    MessageBox.Show("Bạn cần nhập số Serial để kích hoạt chức năng này");
                                else
                                {
                                    string_temp = "select Nhom from dbo.NhapGoLS where Serial=" + txt_Serial_inputLS.Text;
                                    cmd.CommandText = string_temp;
                                    rd = cmd.ExecuteReader();
                                    while (rd.Read())
                                    {
                                        if (rd[0].ToString() == "Phach")
                                        {
                                            rd_Phach_inputLS.Enabled = true;
                                            rd_Phach_inputLS.IsChecked = true;
                                            rd_Van_inputLS.Enabled = false;
                                            rd_GoTron_inputLS.Enabled = false;

                                        }
                                        if (rd[0].ToString() == "Van")
                                        {
                                            rd_Van_inputLS.Enabled = true;
                                            rd_Phach_inputLS.Enabled = false;
                                            rd_Van_inputLS.IsChecked = true;
                                            rd_GoTron_inputLS.Enabled = false;
                                        }
                                        if (rd[0].ToString() == "GoTron")
                                        {
                                            rd_GoTron_inputLS.Enabled = true;
                                            rd_Phach_inputLS.Enabled = false;
                                            rd_Van_inputLS.Enabled = false;
                                            rd_GoTron_inputLS.IsChecked = true;
                                        }
                                        break;

                                    }
                                    MessageBox.Show("Bạn đã kích hoạt chức năng cập nhật thêm hàng hóa vào chứng từ");
                                    lb_CapNhat_inputLS.Visible = true;
                                    lb_Serial_addinputLS.Visible = true;
                                    lb_Serial_addinputLS.Text = txt_Serial_inputLS.Text;
                                    if (radGridView7.Rows.Count > 0)
                                        radGridView7.Rows.Clear();
                                }
                            }

                        }
                        if (Page_Main.SelectedPage == Page_NVL)
                        {
                            if (Page_Other.SelectedPage == Page_input_Other)
                            {
                                if (txt_Serial_inputNVL.Text == "")
                                    MessageBox.Show("Bạn cần nhập số Serial để kích hoạt chức năng này");
                                else
                                {
                                    MessageBox.Show("Bạn đã kích hoạt chức năng cập nhật thêm hàng hóa vào chứng từ");
                                    lb_Serial_DDinputNVL.Visible = true;
                                    lb_Serial_addinputNVL.Visible = true;
                                    lb_Serial_addinputNVL.Text = txt_Serial_inputNVL.Text;
                                    if (radGridView12.Rows.Count > 0)
                                        radGridView12.Rows.Clear();
                                }

                            }
                            if (Page_Other.SelectedPage == Page_output_Other)
                            {
                                if (txt_Serial_outputNVL.Text == "")
                                    MessageBox.Show("Bạn cần nhập số Serial để kích hoạt chức năng này");
                                else
                                {
                                    MessageBox.Show("Bạn đã kích hoạt chức năng cập nhật thêm hàng hóa vào chứng từ");
                                    lb_Serial_DDoutputNVL.Visible = true;
                                    lb_Serial_addoutputNVL.Visible = true;
                                    lb_Serial_addoutputNVL.Text = txt_Serial_outputNVL.Text;
                                    if (radGridView13.Rows.Count > 0)
                                        radGridView13.Rows.Clear();
                                }

                            }

                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    finally
                    {
                        Conn.Close();
                        cmd.Dispose();
                        rd.Close();
                    }
                }
                if (e.KeyCode.Equals(Keys.P))
                {
                    if (Page_Main.SelectedPage == Page_NVL)
                    {
                        if (Page_Other.SelectedPage == Page_input_Other)
                        {
                            print_inputNVL();
                        }
                        if (Page_Other.SelectedPage == Page_output_Other)
                            print_outputNVL();
                    }
                }
                
            }
        }
        #endregion

        private void FrCungTieu_Load(object sender, EventArgs e)
        {
            Load_();
        }
        #region //Nhap Kho Go
        private void Checked_SoChePhach_input()
        {
            cb_QuyCach_inputKG.Enabled = true;
            cb_PhanLoai_inputKG.Enabled = true;
            txt_SLPhach_inputKG.Enabled = true;
            txt_CDay_inputKG.Enabled = false;
            txt_SLVan_inputKG.Enabled = false;
            txt_SoKhoiVan_inputKG.Enabled = false;
            
            txt_CDaiGT_inputKG.Enabled = false;
            txt_VanhGT_inputKG.Enabled = false;
            txt_SLGT_inputKG.Enabled = false;
            radGridView1.Columns["DVD1_inputKG"].HeaderText = "C Dày";
            radGridView1.Columns["DVD2_inputKG"].HeaderText = "C Rộng";
            radGridView1.Columns["DVD3_inputKG"].HeaderText = "C Dài";
            

        }
        private void Checked_Van_input()
        {
            cb_QuyCach_inputKG.Enabled = false;
            cb_PhanLoai_inputKG.Enabled = false;
            txt_SLPhach_inputKG.Enabled = false;
            txt_CDay_inputKG.Enabled = true;
            txt_SLVan_inputKG.Enabled = true;
            txt_SoKhoiVan_inputKG.Enabled = true;

            txt_CDaiGT_inputKG.Enabled = false;
            txt_VanhGT_inputKG.Enabled = false;
            txt_SLGT_inputKG.Enabled = false;
            radGridView1.Columns["DVD1_inputKG"].HeaderText = "C Dày";
            radGridView1.Columns["DVD2_inputKG"].HeaderText = " ";
            radGridView1.Columns["DVD3_inputKG"].HeaderText = " ";
            
        }
        private void Checked_GoTron_input()
        {
            cb_QuyCach_inputKG.Enabled = false;
            cb_PhanLoai_inputKG.Enabled = false;
            txt_SLPhach_inputKG.Enabled = false;
            txt_CDay_inputKG.Enabled = false;
            txt_SLVan_inputKG.Enabled = false;
            txt_SoKhoiVan_inputKG.Enabled = false;

            txt_CDaiGT_inputKG.Enabled = true;
            txt_VanhGT_inputKG.Enabled = true;
            txt_SLGT_inputKG.Enabled = true;
            radGridView1.Columns["DVD2_inputKG"].HeaderText = "C Dài";
            radGridView1.Columns["DVD1_inputKG"].HeaderText = "Vanh";
            radGridView1.Columns["DVD3_inputKG"].HeaderText = " ";

        }
        private void rd_Phach_inputKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_SoChePhach_input();
        }

        private void rd_Van_inputKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_Van_input();
        }

        private void rd_GoTron_inputKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_GoTron_input();
        }
        private void main_info_Disableinput()
        {
            cb_NoiGiao_inputKG.Enabled = false;
            cb_NoiNhan_inputKG.Enabled = false;
            cb_AdorKD_inputKG.Enabled = false;
            cb_LyDo_inputKG.Enabled = false;
            txt_BienBan_inputKG.Enabled = false;
            txt_ChungTu_inputKG.Enabled = false;
            txt_SoXe_inputKG.Enabled = false;
            cb_NguonGo_inputKG.Enabled = false;
        }
        private void main_info_Enableinput()
        {
            cb_NoiGiao_inputKG.Enabled = true;
            cb_NoiNhan_inputKG.Enabled = true;
            cb_AdorKD_inputKG.Enabled = true;
            cb_LyDo_inputKG.Enabled = true;
            txt_BienBan_inputKG.Enabled = true;
            txt_ChungTu_inputKG.Enabled = true;
            txt_SoXe_inputKG.Enabled = true;
            cb_NguonGo_inputKG.Enabled = true;
            rd_GoTron_inputKG.Enabled = true;
            rd_Van_inputKG.Enabled = true;
            rd_Phach_inputKG.Enabled = true;
        }
        private void check_main_infoinput()
        {
            try
            {
                NoiGiao1 = cb_NoiGiao_inputKG.Text;
                NoiNhan1 = cb_NoiNhan_inputKG.Text;
                count = 0;
                for (i = 0; i < cb_NoiNhan_inputKG.Items.Count; i++)
                {
                    if (NoiGiao1 == cb_NoiNhan_inputKG.Items[i].ToString())
                        count++;
                    if (NoiNhan1 == cb_NoiNhan_inputKG.Items[i].ToString())
                        count++;
                    if (count == 2)
                        break;

                }
               // MessageBox.Show(count.ToString()+","+NoiGiao1+","+NoiNhan1);
                if (count < 2 || (cb_AdorKD_inputKG.Text != "AD" && cb_AdorKD_inputKG.Text != "KD"))
                {
                    MessageBox.Show("vui lòng kiểm tra lại thông tin NƠI GIAO hoặc NƠI NHẬN");
                    cb_NoiGiao_inputKG.Text = "";
                    
                    cb_NoiGiao_inputKG.Focus();
                }
                else
                {
                    LyDo1 = cb_LyDo_inputKG.Text;
                    for (i = 0; i < cb_LyDo_inputKG.Items.Count; i++)
                    {
                        if (LyDo1 == cb_LyDo_inputKG.Items[i].Text)
                        {
                            count++;
                            break;
                        }
                    }
                    if (count < 3)
                    {
                        MessageBox.Show("Kiểm tra lại lý do nhập");
                        cb_LyDo_inputKG.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void begin_load_input()
        {
            //rd_Van_inputKG.Enabled = false;
            txt_CDay_inputKG.Enabled = false;
            txt_SLVan_inputKG.Enabled = false;
            txt_SoKhoiVan_inputKG.Enabled = false;
           // rd_GoTron_inputKG.Enabled = false;
            txt_CDaiGT_inputKG.Enabled = false;
            txt_VanhGT_inputKG.Enabled = false;
            txt_SLGT_inputKG.Enabled = false;
        }
        private void cb_QuyCach_inputKG_Leave(object sender, EventArgs e)
        {
            try
            {
                if (cb_QuyCach_inputKG.Text != "")
                {
                    string_temp = prs.substring_right(cb_QuyCach_inputKG.Text, child);
                    string[] temp = string_temp.Split('x');
                    DVD1 = temp[0].ToString().Trim();
                    DVD2 = temp[1].ToString().Trim();
                    DVD3 = temp[2].ToString().Trim();
                    temp.Clone();
                    if (DVD1 == "" || DVD2 == "" || DVD3 == "")
                    {
                        cb_QuyCach_inputKG.Text = "";
                        MessageBox.Show("Bạn đang nhập sai quy cách");
                        cb_QuyCach_inputKG.Focus();
                    }
                    

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       
        /*private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView1);
        }*/
        /*private void radGridView1_UserAddedRow(object sender, GridViewRowEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView1);
        }*/
        private void Addgrid_input()
        {
            
            try
            {
                DVT1 = cb_DVT_inputKG.Text.Trim();
                KhachHang1 = cb_KhachHang_inputKG.Text.Trim();
                GhiChu1 = txt_GhiChu_inputKG.Text;
                count=0;
                /*NoiGiao1 = prs.substring_left(cb_NoiGiao_inputKG.Text, child);
                NoiNhan1 = prs.substring_left(cb_NoiNhan_inputKG.Text, child);
                ADorKD1 = cb_AdorKD_inputKG.Text.Trim();
                PhanLoai1 = cb_PhanLoai_inputKG.Text.Trim();
                LyDo1 = prs.substring_right(cb_LoaiGo_inputKG.Text, child);
                BienBan1 = txt_BienBan_inputKG.Text.Trim();
                ChungTu1 = txt_BienBan_inputKG.Text.Trim();
                Ngay1 = Date_Split(dtp_Date_inputKG.Text);
                SoXe1 = txt_SoXe_inputKG.Text.Trim();
                ChungTu1 = txt_ChungTu_inputKG.Text.Trim();
                */
                if (rd_Phach_inputKG.IsChecked == true)
                {
                    for (i = 0; i < cb_QuyCach_inputKG.Items.Count; i++)
                    {
                        if (cb_QuyCach_inputKG.Text == cb_QuyCach_inputKG.Items[i].ToString())
                        {
                            count++;
                            break;
                        }
                    }
                    if (count == 0)
                        MessageBox.Show("Nhập sai quy cách");
                    else
                    {
                        
                        string_temp = prs.substring_right(cb_QuyCach_inputKG.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                        PhanLoai1 = cb_PhanLoai_inputKG.Text.Trim();
                        SoLuong1 = txt_SLPhach_inputKG.Text.Trim();
                        temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                        SoKhoi1 = temp_double.ToString();
                        
                    }
                    

                }
                if (rd_Van_inputKG.IsChecked == true)
                {
                    
                    DVD1 = txt_CDay_inputKG.Text.Trim();
                    SoLuong1 = txt_SLVan_inputKG.Text.Trim();
                    SoKhoi1 = txt_SoKhoiVan_inputKG.Text.Trim();
                    if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        DVD2 = "";
                        DVD3 = "";
                        count = 1;
                    }
                    
                }
                if (rd_GoTron_inputKG.IsChecked == true)
                {
                    
                    DVD1 = txt_VanhGT_inputKG.Text.Trim();
                    DVD2 = txt_CDaiGT_inputKG.Text.Trim();
                    SoLuong1 = txt_SLGT_inputKG.Text.Trim();

                    if (DVD1 == "" || DVD2 == "" || SoLuong1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        temp_double = double.Parse(DVD1) * double.Parse(DVD1) * double.Parse(DVD2) *double.Parse(SoLuong1)/ (4 * 3.1415926 * 1000000000);
                        SoKhoi1 = temp_double.ToString();
                        count = 1;
                        DVD3 = "";
                    }
                }
                if (count == 1)
                {
                    //RowFormattingEventArgs e = null;
                    //e.RowElement.DrawFill=true;
                    int_temp = radGridView1.Rows.Count;
                    if (int_temp > 0)
                    {  
                        i = int.Parse(radGridView1.Rows[int_temp - 1].Cells["STT_inputKG"].Value.ToString()) + 1;
                        
                        radGridView1.Rows.Add(i.ToString(), DVD1, DVD2, DVD3, SoLuong1, SoKhoi1, PhanLoai1, DVT1, KhachHang1, GhiChu1, "", "");
                        //if (i % 2 == 0)
                          //  e.RowElement.BackColor = System.Drawing.Color.AliceBlue;
                    }
                    else
                    {
                        radGridView1.Rows.Add("1", DVD1, DVD2, DVD3, SoLuong1, SoKhoi1, PhanLoai1, DVT1, KhachHang1, GhiChu1, "", "");
                    }
                    //temp_double=double.Parse(
                    cb_QuyCach_inputKG.Text = "";
                    cb_PhanLoai_inputKG.Text = "";
                    txt_SLPhach_inputKG.Text = "";
                    txt_CDay_inputKG.Text = "";
                    txt_SLVan_inputKG.Text = "";
                    txt_SoKhoiVan_inputKG.Text = "";
                    txt_CDaiGT_inputKG.Text = "";
                    txt_VanhGT_inputKG.Text = "";
                    txt_SLGT_inputKG.Text = "";
                    txt_QC1_inputKG.Text = "";
                    txt_QC2_inputKG.Text = "";
                    txt_QC3_inputKG.Text = "";
                    if (cb_NoiGiao_inputKG.Enabled == true)
                    {
                        check_main_infoinput();
                        if(count>=4)
                            main_info_Disableinput();
                        if (rd_Phach_inputKG.IsChecked == true)
                        {
                            rd_GoTron_inputKG.Enabled = false;
                            rd_Van_inputKG.Enabled = false;
                            txt_CDay_inputKG.Enabled = false;
                            txt_SLVan_inputKG.Enabled = false;
                            txt_SoKhoiVan_inputKG.Enabled = false;
                            txt_CDaiGT_inputKG.Enabled = false;
                            txt_VanhGT_inputKG.Enabled = false;
                            txt_SLGT_inputKG.Enabled = false;
                            //cb_QuyCach_inputKG.Focus();
                            txt_QC1_inputKG.Focus();
                            
                        }
                        if (rd_Van_inputKG.IsChecked == true)
                        {
                            rd_GoTron_inputKG.Enabled = false;
                            rd_Phach_inputKG.Enabled = false;
                            txt_CDaiGT_inputKG.Enabled = false;
                            txt_VanhGT_inputKG.Enabled = false;
                            txt_SLGT_inputKG.Enabled = false;
                            cb_QuyCach_inputKG.Enabled = false;
                            txt_SLPhach_inputKG.Enabled = false;
                            cb_PhanLoai_inputKG.Enabled = false;
                            txt_CDay_inputKG.Focus();
                        }
                        if (rd_GoTron_inputKG.IsChecked == true)
                        {
                            rd_Van_inputKG.Enabled = false;
                            rd_Phach_inputKG.Enabled = false;
                            cb_QuyCach_inputKG.Enabled = false;
                            txt_SLPhach_inputKG.Enabled = false;
                            cb_PhanLoai_inputKG.Enabled = false;
                            txt_CDay_inputKG.Enabled = false;
                            txt_SLVan_inputKG.Enabled = false;
                            txt_SoKhoiVan_inputKG.Enabled = false;
                            txt_VanhGT_inputKG.Focus();
                        }
                    }
                    if (rd_Phach_inputKG.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_inputKG.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_inputKG.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_inputKG.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_inputKG.Text = string.Format("{0:###,###,###}", temp_double);
                        //cb_QuyCach_inputKG.Focus();
                        txt_QC1_inputKG.Focus();
                    }
                    if (rd_Van_inputKG.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_inputKG.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_inputKG.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_inputKG.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_inputKG.Text = string.Format("{0:###,###,###.#}", temp_double);
                        txt_VanhGT_inputKG.Focus();
                    }
                    if (rd_GoTron_inputKG.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_inputKG.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_inputKG.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_inputKG.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_inputKG.Text = temp_double.ToString();
                        txt_SLGT_inputKG.Text = "1";
                        txt_VanhGT_inputKG.Focus();
                    }
                    
                }

                //radGridView1_RowFormatting(null, null, int_temp);


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        private void dtp_Date_inputKG_Leave(object sender, EventArgs e)
        {
            
        }
        private void bt_add_inputKG_Click(object sender, EventArgs e)
        {
            Addgrid_input();
        }
       
        private void radGridView1_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView1.Rows.Count > 0)
            {
                try
                {
                    //MessageBox.Show(radGridView1.Rows.Count.ToString());
                    int r = radGridView1.CurrentCell.RowIndex;

                    temp_double = double.Parse(lb_TongSL_inputKG.Text) - double.Parse(radGridView1.Rows[r].Cells["SoLuong_inputKG"].Value.ToString());
                    lb_TongSL_inputKG.Text = string.Format("{0:###,###}", temp_double);
                    temp_double = double.Parse(lb_TongKhoi_inputKG.Text) - double.Parse(radGridView1.Rows[r].Cells["SoKhoi_inputKG"].Value.ToString());
                    lb_TongKhoi_inputKG.Text = string.Format("{0:###,###.####}", temp_double);
                    radGridView1.Rows.RemoveAt(r);

                    j = radGridView1.Rows.Count - 1;
                    //radGridView1.CurrentCell = radGridView1.Rows[j].Cells["MaCTinput"];

                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (radGridView1.Rows.Count == 0)
            {
                lb_TongKhoi_inputKG.Text = "0";
                lb_TongSL_inputKG.Text = "0";
            }
        }
        private void print_inputKG()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1="";
            DataSet ds = new DataSet();
            try
            {
                 Ngay1=Date_Split(dtp_Date_inputKG.Text);
                if (rd_Phach_inputKG.IsChecked == true)
                {
                    string_temp = @" select dbo.ChungTuNhap.Serial,ChungTuNhap.NoiGiao,ChungTuNhap.NoiNhan,ChungTuNhap.ADorKD,ChungTuNhap.BienBan as SoBB,ChungTuNhap.ChungTu,ChungTuNhap.LoaiGo,ChungTuNhap.LyDo,ChungTuNhap.Serial,convert(varchar(10),ChungTuNhap.Ngay,103) as Ngay
                                ,dbo.Nhap_Phach.CDai,Nhap_Phach.CDay,Nhap_Phach.CRong,Nhap_Phach.KhachHang,Nhap_Phach.SLNhap,(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,Nhap_Phach.SLXuat,(CDay*CRong*CDai*SLXuat)/1000000000 as SoKhoiXuat,Nhap_Phach.PhanLoai
                                from dbo.Nhap_Phach inner join dbo.ChungTuNhap on (dbo.Nhap_Phach.Serial_ChungTu=ChungTuNhap.Serial";
                    if (cb_QuyCach_inputKG.Text != "")
                    {
                        string Ngay_temp = prs.substring_right(cb_QuyCach_inputKG.Text, child);
                        string[] temp = Ngay_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                       
                        GhiChu1 += " and Nhap_Phach.CDay=" + DVD1 + " and Nhap_Phach.CRong=" + DVD2 + " and Nhap_Phach.CDai=" + DVD3;
                    }
                    if (cb_PhanLoai_inputKG.Text != "")
                        GhiChu1 += "and Nhap_Phach.PhanLoai='" + cb_PhanLoai_inputKG.Text + "'";
                }
                if (rd_Van_inputKG.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuNhap.Serial,ChungTuNhap.NoiGiao,ChungTuNhap.NoiNhan,ChungTuNhap.ADorKD,ChungTuNhap.BienBan as SoBB,ChungTuNhap.ChungTu,ChungTuNhap.LoaiGo,ChungTuNhap.LyDo,ChungTuNhap.Serial,convert(varchar(10),ChungTuNhap.Ngay,103) as Ngay
                                ,Nhap_Van.CDay,Nhap_Van.KhachHang,Nhap_Van.SLNhap,SoKhoiNhap,Nhap_Van.SLXuat,SoKhoiXuat
                                from dbo.Nhap_Van inner join dbo.ChungTuNhap on (dbo.Nhap_Van.Serial_ChungTu=ChungTuNhap.Serial";
                    if (txt_CDay_inputKG.Text != "")
                        GhiChu1 += " and Nhap_Van.CDay=" + txt_CDay_inputKG.Text;
                }
                if (rd_GoTron_inputKG.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuNhap.Serial,ChungTuNhap.NoiGiao,ChungTuNhap.NoiNhan,ChungTuNhap.ADorKD,ChungTuNhap.BienBan as SoBB,ChungTuNhap.ChungTu,ChungTuNhap.LoaiGo,ChungTuNhap.LyDo,ChungTuNhap.Serial,convert(varchar(10),ChungTuNhap.Ngay,103) as Ngay
                                ,dbo.Nhap_GoTron.CDai,Nhap_GoTron.Vanh,Nhap_GoTron.KhachHang,Nhap_GoTron.SLNhap,(Vanh*Vanh*CDai*SLNhap)/(4*3.141593*1000000000) as SoKhoiNhap,Nhap_GoTron.SLXuat,(Vanh*Vanh*CDai*SLXuat)/(4*3.141593*1000000000) as SoKhoiXuat
                                from dbo.Nhap_GoTron inner join dbo.ChungTuNhap on (dbo.Nhap_GoTron.Serial_ChungTu=ChungTuNhap.Serial";
                    if (txt_VanhGT_inputKG.Text != "")
                        GhiChu1 += " and Nhap_GoTron.Vanh=" + txt_VanhGT_inputKG.Text;
                    if (txt_CDaiGT_inputKG.Text != "")
                        GhiChu1 += " and Nhap_GoTron.CDai=" + txt_CDaiGT_inputKG.Text;
                }
                if (cb_NoiGiao_inputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.NoiGiao='" + prs.substring_left(cb_NoiGiao_inputKG.Text, child) + "'";
                if (cb_NoiNhan_inputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.NoiNhan='" + prs.substring_left(cb_NoiNhan_inputKG.Text, child)+"'";
                if (cb_NguonGo_inputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.LoaiGo='" + cb_NguonGo_inputKG.Text + "'";
                if (cb_LyDo_inputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.LyDo='" + prs.substring_right(cb_LyDo_inputKG.Text, child) + "'";
                if (txt_ChungTu_inputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.ChungTu='" + txt_ChungTu_inputKG.Text + "'";
                if (txt_BienBan_inputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.BienBan='" + txt_BienBan_inputKG.Text + "'";
                if (cb_AdorKD_inputKG.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_inputKG.Text + "'";
                string_temp += GhiChu1 + " and SLNhap<>0 and ChungTuNhap.Ngay='" + Ngay1 + "')";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Date_inputKG.Text;
               
                // FrR.report_KV1NKTG(ds, Ngay1);

                if (rd_Phach_inputKG.IsChecked == true||rd_Van_inputKG.IsChecked==true)
                {
                    FrR.CrR_NK_PhachSC(ds, Ngay1);
                }
                if (rd_GoTron_inputKG.IsChecked == true)
                    FrR.CrR_NK_GoTron(ds, Ngay1);
                
                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }
        private void print_TH_inputKG()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Date_inputKG.Text);

                string_temp = @" SELECT [NoiGiao],[NoiNhan],[ADorKD],[LoaiGo] ,[LyDo] ,[BienBan] as SoBB,[ChungTu],[Ngay] ,[DVD1],[DVD2],[DVD3],[PhanLoai],[SLNhap] ,[SoKhoiNhap]
                                     ,[KhachHang],[GhiChu],[Serial_ChungTu] as Serial
                                    FROM [Planning].[dbo].[NhapGo] where SLNhap<>0 ";

                if (cb_NoiGiao_inputKG.Text != "")
                    GhiChu1 += " and NoiGiao='" + prs.substring_left(cb_NoiGiao_inputKG.Text, child) + "'";
                if (cb_NoiNhan_inputKG.Text != "")
                    GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_inputKG.Text, child) + "'";
                if (cb_NguonGo_inputKG.Text != "")
                    GhiChu1 += " and LoaiGo='" + cb_NguonGo_inputKG.Text + "'";
                if (cb_LyDo_inputKG.Text != "")
                    GhiChu1 += " and LyDo='" + prs.substring_right(cb_LyDo_inputKG.Text, child) + "'";
                if (txt_ChungTu_inputKG.Text != "")
                    GhiChu1 += " and ChungTu='" + txt_ChungTu_inputKG.Text + "'";
                if (txt_BienBan_inputKG.Text != "")
                    GhiChu1 += " and BienBan='" + txt_BienBan_inputKG.Text + "'";
                if (cb_AdorKD_inputKG.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_inputKG.Text + "'";
                string_temp += GhiChu1 + " and Ngay='" + Ngay1 + "'";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Date_inputKG.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);

              
               FrR.CrR_KG_NhapKho(ds, Ngay1);
              

                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }
        private void cb_LyDo_inputKG_Leave(object sender, EventArgs e)
        {
            /*check_main_infoinput();*/
        }
        private void save_input()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Tran_Ins_CTNhap";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            
            try
            {
                
                //string insert_NhapKTam = null;
                if (lb_Serial_addinput.Text=="0"&& (cb_NoiGiao_inputKG.Text == "" || cb_NoiNhan_inputKG.Text == "" || cb_AdorKD_inputKG.Text == "" || cb_NguonGo_inputKG.Text == "" || cb_LyDo_inputKG.Text == "" || txt_BienBan_inputKG.Text == "" || txt_ChungTu_inputKG.Text == ""||radGridView1.Rows.Count==0))
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    if (lb_Serial_addinput.Text == "0" || lb_Serial_addinput.Visible == false)
                    {
                        NoiGiao1 = prs.substring_left(cb_NoiGiao_inputKG.Text, child);
                        NoiNhan1 = prs.substring_left(cb_NoiNhan_inputKG.Text, child);
                        ADorKD1 = cb_AdorKD_inputKG.Text.Trim();
                        LoaiGo1 = cb_NguonGo_inputKG.Text.Trim();
                        LyDo1 = prs.substring_right(cb_LyDo_inputKG.Text, child);
                        BienBan1 = txt_BienBan_inputKG.Text.Trim();
                        ChungTu1 = txt_ChungTu_inputKG.Text.Trim();
                        SoXe1 = txt_SoXe_inputKG.Text.Trim();
                        Ngay1 = Date_Split(dtp_Date_inputKG.Text);

                        cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                        cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                        cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                        cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                        cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                        cmd.Parameters.AddWithValue("@SoXe", SoXe1);
                        cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                        cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                        SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                        code.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(code);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                        sdr.Close();
                        cmd.Parameters.Clear();
                    }
                    else
                        Serial1 = lb_Serial_addinput.Text;
                    //MessageBox.Show("aaaa");
                    if (rd_Phach_inputKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapPhach";
                        i = 0;
                        while(radGridView1.Rows.Count>0)
                        {
                            DVD1 = radGridView1.Rows[i].Cells["DVD1_inputKG"].Value.ToString();
                            DVD2 = radGridView1.Rows[i].Cells["DVD2_inputKG"].Value.ToString();
                            DVD3 = radGridView1.Rows[i].Cells["DVD3_inputKG"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView1.Rows[i].Cells["SoLuong_inputKG"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView1.Rows[i].Cells["SoKhoi_inputKG"].Value.ToString();
                            PhanLoai1 = radGridView1.Rows[i].Cells["PhanLoai_inputKG"].Value.ToString();
                            DVT1 = radGridView1.Rows[i].Cells["DVT_inputKG"].Value.ToString();
                            KhachHang1 = radGridView1.Rows[i].Cells["KhachHang_inputKG"].Value.ToString();
                            GhiChu1 = radGridView1.Rows[i].Cells["GhiChu_inputKG"].Value.ToString();
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            
                            cmd.Parameters.AddWithValue("@Wait", "0");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView1.Rows.RemoveAt(i);
                        }
                    }

                    if (rd_Van_inputKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapVan";
                        i = 0;
                        while (radGridView1.Rows.Count>0)
                        {
                            DVD1 = radGridView1.Rows[i].Cells["DVD1_inputKG"].Value.ToString();
                            // DVD2 = radGridView1.Rows[i].Cells["DVT2_inputKG"].Value.ToString();
                            //DVD3 = radGridView1.Rows[i].Cells["DVT3_inputKG"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView1.Rows[i].Cells["SoLuong_inputKG"].Value.ToString()).ToString();
                            SoKhoi1 = radGridView1.Rows[i].Cells["SoKhoi_inputKG"].Value.ToString();
                            //PhanLoai1 = radGridView1.Rows[i].Cells["PhanLoai_inputKG"].Value.ToString();
                            DVT1 = radGridView1.Rows[i].Cells["DVT_inputKG"].Value.ToString();
                            KhachHang1 = radGridView1.Rows[i].Cells["KhachHang_inputKG"].Value.ToString();
                            GhiChu1 = radGridView1.Rows[i].Cells["GhiChu_inputKG"].Value.ToString();
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", SoKhoi1);
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                           
                            cmd.Parameters.AddWithValue("@Wait", "0");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView1.Rows.RemoveAt(i);
                        }


                    }
                    if (rd_GoTron_inputKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapGT";
                        i = 0;
                        while (radGridView1.Rows.Count>0)
                        {
                            DVD1 = double.Parse(radGridView1.Rows[i].Cells["DVD1_inputKG"].Value.ToString()).ToString();
                            DVD2 = double.Parse(radGridView1.Rows[i].Cells["DVD2_inputKG"].Value.ToString()).ToString();
                            //DVD3 = radGridView1.Rows[i].Cells["DVT3_inputKG"].Value.ToString();
                            SoLuong1 = SoLuong1 = double.Parse(radGridView1.Rows[i].Cells["SoLuong_inputKG"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView1.Rows[i].Cells["SoKhoi_inputKG"].Value.ToString();
                            //PhanLoai1 = radGridView1.Rows[i].Cells["PhanLoai_inputKG"].Value.ToString();
                            DVT1 = radGridView1.Rows[i].Cells["DVT_inputKG"].Value.ToString();
                            KhachHang1 = radGridView1.Rows[i].Cells["KhachHang_inputKG"].Value.ToString();
                            GhiChu1 = radGridView1.Rows[i].Cells["GhiChu_inputKG"].Value.ToString();


                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                           
                            cmd.Parameters.AddWithValue("@Wait", "0");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView1.Rows.RemoveAt(i);
                        }
                    }
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    bt_rework_inputKG_Click(null, null);
                }
            }

            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
           
        }

        private void txt_CDay_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_CDay_inputKG);
        }

        private void txt_SLPhach_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLPhach_inputKG);
        }

        private void txt_SLVan_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLVan_inputKG);
        }

        private void txt_SoKhoiVan_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SoKhoiVan_inputKG);
        }
        private void txt_QC3_inputKG_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txt_QC3_inputKG.Text != "")
                {
                    string_temp = txt_QC1_inputKG.Text.Trim() + txt_QC2_inputKG.Text.Trim() + txt_QC3_inputKG.Text.Trim();
                    for (i = 0; i < dataGridView_QuyCach.Rows.Count-1; i++)
                    {
                        if (string_temp == dataGridView_QuyCach.Rows[i].Cells["QuyCach"].Value.ToString())
                        {
                            cb_PhanLoai_inputKG.Text = dataGridView_QuyCach.Rows[i].Cells["PhanLoai"].Value.ToString();
                            cb_QuyCach_inputKG.Text=dataGridView_QuyCach.Rows[i].Cells["QuyCach"].Value.ToString()+" - "+dataGridView_QuyCach.Rows[i].Cells["size"].Value.ToString();
                            break;
                        }
                        
                    }
                    if (i == dataGridView_QuyCach.Rows.Count-1)
                    {
                        MessageBox.Show("Bạn đang nhập sai quy cách");
                        cb_QuyCach_inputKG.Text = "";
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void txt_CDaiGT_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_CDaiGT_inputKG);
        }

        private void txt_VanhGT_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_VanhGT_inputKG);
        }

        private void txt_SLGT_inputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLGT_inputKG);
        }
        private void bt_save_inputKG_Click(object sender, EventArgs e)
        {
            save_input();
        }
        private void cb_AdorKD_inputKG_Leave(object sender, EventArgs e)
        {
            if (cb_AdorKD_inputKG.Text != "AD" && cb_AdorKD_inputKG.Text != "KD")
            {
                MessageBox.Show("Nhập sai thông tin");
                cb_AdorKD_inputKG.Text = "";
                cb_AdorKD_inputKG.Focus();
            }
        }

        private void bt_rework_inputKG_Click(object sender, EventArgs e)
        {
            main_info_Enableinput();
            prs.reset_page_txt(Page_NhapGo);
            cb_NoiGiao_inputKG.Focus();
            if (radGridView1.Rows.Count > 0)
            {
                radGridView1.Rows.Clear();
                
            }
            rd_GoTron_inputKG.Enabled = true;
            rd_Phach_inputKG.Enabled = true;
            rd_Van_inputKG.Enabled = true;
            lb_Serial_input.Text = "0";
            lb_TongKhoi_inputKG.Text = "0";
            lb_TongSL_inputKG.Text = "0";
            lb_Serial_addinput.Text="0";
            lb_CapNhat_input.Visible = false;
            lb_Serial_addinput.Visible= false;
            bt_edit_inputKG.Enabled = false;
            bt_delete_inputKG.Enabled = false;
            bt_save_inputKG.Enabled = true;
        }
        private void Display_input(int row)
        {
            //cb_NoiGiao_inputKG.Text = radGridView1.Rows[i].Cells["NoiGiao"].Value.ToString();

            if (radGridView1.Rows[row].Cells["Nhom_input"].Value.ToString() != "" && radGridView1.Rows[row].Cells["Wait_input"].Value.ToString() != "2" && radGridView1.Rows[row].Cells["SoLuong_inputKG"].Value.ToString()!="0")
            {

                //MessageBox.Show(radGridView1.Rows[row].Cells["Nhom_input"].Value.ToString());
                if (radGridView1.Rows[row].Cells["Nhom_input"].Value.ToString() == "Phach")
                {
                    rd_Phach_inputKG.Enabled = true;
                    rd_Phach_inputKG.IsChecked = true;
                    rd_Van_inputKG.Enabled = false;
                    rd_GoTron_inputKG.Enabled = false;
                    cb_QuyCach_inputKG.Text = radGridView1.Rows[row].Cells["DVD1_inputKG"].Value.ToString() + radGridView1.Rows[row].Cells["DVD2_inputKG"].Value.ToString() + radGridView1.Rows[row].Cells["DVD3_inputKG"].Value.ToString() + " - " + radGridView1.Rows[row].Cells["DVD1_inputKG"].Value.ToString() + "x" + radGridView1.Rows[row].Cells["DVD2_inputKG"].Value.ToString() + "x" + radGridView1.Rows[row].Cells["DVD3_inputKG"].Value.ToString();
                    cb_PhanLoai_inputKG.Text = radGridView1.Rows[row].Cells["PhanLoai_inputKG"].Value.ToString();
                    txt_SLPhach_inputKG.Text = radGridView1.Rows[row].Cells["SoLuong_inputKG"].Value.ToString();

                }
                if (radGridView1.Rows[row].Cells["Nhom_input"].Value.ToString() == "Van")
                {
                    rd_Van_inputKG.Enabled = true;
                    rd_Van_inputKG.IsChecked = true;
                    rd_Phach_inputKG.Enabled = false;
                    rd_GoTron_inputKG.Enabled = false;
                    txt_CDay_inputKG.Text = radGridView1.Rows[row].Cells["DVD1_inputKG"].Value.ToString();
                    txt_SLVan_inputKG.Text = radGridView1.Rows[row].Cells["SoLuong_inputKG"].Value.ToString();
                    txt_SoKhoiVan_inputKG.Text = radGridView1.Rows[row].Cells["SoKhoi_inputKG"].Value.ToString();
                }
                if (radGridView1.Rows[row].Cells["Nhom_input"].Value.ToString() == "GoTron")
                {
                    rd_GoTron_inputKG.Enabled = true;
                    rd_GoTron_inputKG.IsChecked = true;
                    rd_Phach_inputKG.Enabled = false;
                    rd_Van_inputKG.Enabled = false;
                    txt_CDaiGT_inputKG.Text = radGridView1.Rows[row].Cells["DVD2_inputKG"].Value.ToString();
                    txt_VanhGT_inputKG.Text = radGridView1.Rows[row].Cells["DVD1_inputKG"].Value.ToString();
                    txt_SLGT_inputKG.Text = radGridView1.Rows[row].Cells["SoLuong_inputKG"].Value.ToString();
                }
                cb_DVT_inputKG.Text = radGridView1.Rows[row].Cells["DVT_inputKG"].Value.ToString();
                cb_KhachHang_inputKG.Text = radGridView1.Rows[row].Cells["KhachHang_inputKG"].Value.ToString();
                lb_Serial_input.Text = radGridView1.Rows[row].Cells["Serial_inputKG"].Value.ToString();
                lb_index.Text = row.ToString();
            }
            if (radGridView1.Rows[row].Cells["Wait_input"].Value.ToString() == "2")
                MessageBox.Show("Chi tiết này thuộc nhóm chuyển, liên hệ quản trị viên nếu muốn sửa");
           
        }
        private void search_inputKG()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = "prc_Search_KGinput";
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                lb_Serial_input.Text = "0";
                
                Serial1 = txt_Serial_input.Text;
                cmd.Parameters.AddWithValue("@Serial", Serial1);
                if (radGridView1.Rows.Count > 0)
                    radGridView1.Rows.Clear();
                DataSet ds = new DataSet();
                ds=prs.dts_sql(cmd, Conn, ds);
                prs.reset_page_txt(Page_NhapGo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    cb_NoiGiao_inputKG.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                    cb_NoiNhan_inputKG.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                    cb_AdorKD_inputKG.Text = ds.Tables[0].Rows[0]["ADorKD"].ToString();
                    cb_NguonGo_inputKG.Text = ds.Tables[0].Rows[0]["LoaiGo"].ToString();
                    cb_LyDo_inputKG.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                    txt_BienBan_inputKG.Text = ds.Tables[0].Rows[0]["BienBan"].ToString();
                    txt_ChungTu_inputKG.Text = ds.Tables[0].Rows[0]["ChungTu"].ToString();
                    txt_SoXe_inputKG.Text = ds.Tables[0].Rows[0]["SoXe"].ToString();
                    dtp_Date_inputKG.Text = ds.Tables[0].Rows[0]["Ngay"].ToString();
                    main_info_Disableinput();
                    temp_double = 0;
                    SoKhoi_d = 0;
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        radGridView1.Rows.Add((i+1).ToString(),ds.Tables[0].Rows[i]["DVD1"].ToString(), ds.Tables[0].Rows[i]["DVD2"].ToString(), ds.Tables[0].Rows[i]["DVD3"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString(), ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString(), ds.Tables[0].Rows[i]["PhanLoai"].ToString(), ds.Tables[0].Rows[i]["DVT"].ToString(), ds.Tables[0].Rows[i]["KhachHang"].ToString(), ds.Tables[0].Rows[i]["GhiChu"].ToString(),ds.Tables[0].Rows[i]["Wait"].ToString(), ds.Tables[0].Rows[i]["Nhom"].ToString(), ds.Tables[0].Rows[i]["ID"].ToString());
                        temp_double += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                        SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString());
                    }
                    if (SoKhoi_d == 0)
                        lb_TongKhoi_inputKG.Text = "0";
                    else
                        lb_TongKhoi_inputKG.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                    if (temp_double == 0)
                        lb_TongSL_inputKG.Text = "0";
                    else
                        lb_TongSL_inputKG.Text = string.Format("{0:###,###}", temp_double);
                }
                else
                    MessageBox.Show("Không có dữ liệu vui lòng kiểm tra lại thông tin tìm kiếm");
                ds.Clear();
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("Kiểm tra lại Serial " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_Search_inputKG_Click(object sender, EventArgs e)
        {
            search_inputKG();
            bt_save_inputKG.Enabled = false;
            bt_edit_inputKG.Enabled = true;
            bt_delete_inputKG.Enabled = true;
        }

        private void radGridView1_Click(object sender, EventArgs e)
        {
            if (radGridView1.Rows.Count > 0)
            {
                int r = radGridView1.CurrentRow.Index;
                Display_input(r);
            }
        }

        private void bt_Prev_inputKG_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView1.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_input(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_inputKG_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView1.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i < radGridView1.Rows.Count - 1)
                    {
                        i++;
                        Display_input(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void edit_input()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            DVT1 = cb_DVT_inputKG.Text.Trim();
            KhachHang1 = cb_KhachHang_inputKG.Text.Trim();
            GhiChu1 = txt_GhiChu_inputKG.Text;
            count = 0;
            cmd.CommandType = CommandType.StoredProcedure;
            Serial1 = lb_Serial_input.Text;
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (TL == DialogResult.Yes)
            {
                try
                {
                    if (rd_Phach_inputKG.IsChecked == true)
                    {
                        for (i = 0; i < cb_QuyCach_inputKG.Items.Count; i++)
                        {
                            if (cb_QuyCach_inputKG.Text == cb_QuyCach_inputKG.Items[i].ToString())
                            {
                                count++;
                                break;
                            }
                        }
                        if (count == 0)
                            MessageBox.Show("Nhập sai quy cách");
                        else
                        {
                            count = 0;
                            string_temp = prs.substring_right(cb_QuyCach_inputKG.Text, child);
                            string[] temp = string_temp.Split('x');
                            DVD1 = temp[0].ToString().Trim();
                            DVD2 = temp[1].ToString().Trim();
                            DVD3 = temp[2].ToString().Trim();
                            temp.Clone();
                            PhanLoai1 = cb_PhanLoai_inputKG.Text.Trim();
                            SoLuong1 = double.Parse(txt_SLPhach_inputKG.Text.Trim()).ToString();
                            //temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                            //SoKhoi1 = temp_double.ToString();
                            cmd.CommandText = "UpdNhapPhach";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            count = 1;
                        }


                    }
                    if (rd_Van_inputKG.IsChecked == true)
                    {

                        DVD1 = txt_CDay_inputKG.Text.Trim();
                        SoLuong1 = double.Parse(txt_SLVan_inputKG.Text.Trim()).ToString();
                        SoKhoi1 = txt_SoKhoiVan_inputKG.Text.Trim();
                        if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            cmd.CommandText = "UpdNhapVan";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", SoKhoi1);
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            count = 1;
                        }
                    }
                    if (rd_GoTron_inputKG.IsChecked == true)
                    {
                        DVD2 = double.Parse(txt_CDaiGT_inputKG.Text.Trim()).ToString();
                        DVD1 = double.Parse(txt_VanhGT_inputKG.Text.Trim()).ToString();
                        SoLuong1 = double.Parse(txt_SLGT_inputKG.Text.Trim()).ToString();

                        if (DVD3 == "" || DVD2 == "" || SoLuong1 == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            cmd.CommandText = "UpdNhapGT";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            //temp_double = double.Parse(DVD2) * double.Parse(DVD2) * double.Parse(DVD1) / (4 * 3.1415926 * 1000000000);
                            //SoKhoi1 = temp_double.ToString();
                            count = 1;
                        }
                    }
                    if (count == 1)
                    {
                        MessageBox.Show("Chỉnh sửa thành công");
                        bt_rework_inputKG_Click(null, null);
                    }

                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void bt_edit_inputKG_Click(object sender, EventArgs e)
        {
            edit_input();
        }
        private void delete_input()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                DialogResult TL;
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA ID: " + lb_Serial_input.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (TL == DialogResult.Yes)
                {
                    if (rd_Phach_inputKG.IsChecked == true)
                    {
                        string_temp = @"DELETE FROM [dbo].[Nhap_Phach] WHERE Serial="+lb_Serial_input.Text;
                    }
                    if (rd_Van_inputKG.IsChecked == true)
                    {
                        string_temp = @"DELETE FROM [dbo].[Nhap_Van] WHERE Serial=" + lb_Serial_input.Text;
                    }
                    if (rd_GoTron_inputKG.IsChecked == true)
                    {
                        string_temp = @"DELETE FROM [dbo].[Nhap_GoTron] WHERE Serial=" + lb_Serial_input.Text;
                    }
                    cmd.CommandText = string_temp;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa thành công");
                    bt_rework_inputKG_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("ĐÃ XẢY RA LỖI. DỮ LIỆU XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_delete_inputKG_Click(object sender, EventArgs e)
        {
            delete_input();
        }

        #endregion

       
        #region //Xuat Kho
       private void Checked_SoChePhach_output()
        {
            cb_QuyCach_outputKG.Enabled = true;
            cb_PhanLoai_outputKG.Enabled = true;
            txt_SLPhach_outputKG.Enabled = true;
            txt_CDay_outputKG.Enabled = false;
            txt_SLVan_outputKG.Enabled = false;
            txt_SoKhoiVan_outputKG.Enabled = false;
            
            txt_CDaiGT_outputKG.Enabled = false;
            txt_VanhGT_outputKG.Enabled = false;
            txt_SLGT_outputKG.Enabled = false;
            radGridView2.Columns["DVD1_outputKG"].HeaderText = "C Dày";
            radGridView2.Columns["DVD2_outputKG"].HeaderText = "C Rộng";
            radGridView2.Columns["DVD3_outputKG"].HeaderText = "C Dài";
            

        }
        private void Checked_Van_output()
        {
            cb_QuyCach_outputKG.Enabled = false;
            cb_PhanLoai_outputKG.Enabled = false;
            txt_SLPhach_outputKG.Enabled = false;
            txt_CDay_outputKG.Enabled = true;
            txt_SLVan_outputKG.Enabled = true;
            txt_SoKhoiVan_outputKG.Enabled = true;

            txt_CDaiGT_outputKG.Enabled = false;
            txt_VanhGT_outputKG.Enabled = false;
            txt_SLGT_outputKG.Enabled = false;
            radGridView2.Columns["DVD1_outputKG"].HeaderText = "C Dày";
            radGridView2.Columns["DVD2_outputKG"].HeaderText = " ";
            radGridView2.Columns["DVD3_outputKG"].HeaderText = " ";
            
        }
        private void Checked_GoTron_output()
        {
            cb_QuyCach_outputKG.Enabled = false;
            cb_PhanLoai_outputKG.Enabled = false;
            txt_SLPhach_outputKG.Enabled = false;
            txt_CDay_outputKG.Enabled = false;
            txt_SLVan_outputKG.Enabled = false;
            txt_SoKhoiVan_outputKG.Enabled = false;

            txt_CDaiGT_outputKG.Enabled = true;
            txt_VanhGT_outputKG.Enabled = true;
            txt_SLGT_outputKG.Enabled = true;
            radGridView2.Columns["DVD1_outputKG"].HeaderText = "Vanh";
            radGridView2.Columns["DVD2_outputKG"].HeaderText = "C Dài";
            radGridView2.Columns["DVD3_outputKG"].HeaderText = " ";

        }
        private void rd_Phach_outputKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_SoChePhach_output();
        }

        private void rd_Van_outputKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_Van_output();
        }

        private void rd_GoTron_outputKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_GoTron_output();
        }
        private void main_info_Disableoutput()
        {
            cb_NoiGiao_outputKG.Enabled = false;
            cb_NoiNhan_outputKG.Enabled = false;
            cb_AdorKD_outputKG.Enabled = false;
            cb_LyDo_outputKG.Enabled = false;
            txt_BienBan_outputKG.Enabled = false;
            txt_ChungTu_outputKG.Enabled = false;
            txt_SoXe_outputKG.Enabled = false;
            cb_NguonGo_outputKG.Enabled = false;
        }
        private void main_info_Enableoutput()
        {
            cb_NoiGiao_outputKG.Enabled = true;
            cb_NoiNhan_outputKG.Enabled = true;
            cb_AdorKD_outputKG.Enabled = true;
            cb_LyDo_outputKG.Enabled = true;
            txt_BienBan_outputKG.Enabled = true;
            txt_ChungTu_outputKG.Enabled = true;
            txt_SoXe_outputKG.Enabled = true;
            cb_NguonGo_outputKG.Enabled = true;
            rd_GoTron_outputKG.Enabled = true;
            rd_Van_outputKG.Enabled = true;
            rd_Phach_outputKG.Enabled = true;
           
        }
        private void check_main_infooutput()
        {
            try
            {
                NoiGiao1 = cb_NoiGiao_outputKG.Text;
                NoiNhan1 = cb_NoiNhan_outputKG.Text;
                count = 0;
                for (i = 0; i < cb_NoiNhan_outputKG.Items.Count; i++)
                {
                    if (NoiGiao1 == cb_NoiNhan_outputKG.Items[i].ToString())
                        count++;
                    if (NoiNhan1 == cb_NoiNhan_outputKG.Items[i].ToString())
                        count++;
                    if (count == 2)
                        break;

                }
                if (count < 2 || (cb_AdorKD_outputKG.Text != "AD" && cb_AdorKD_outputKG.Text != "KD"))
                {
                    MessageBox.Show("vui lòng kiểm tra lại thông tin NƠI GIAO hoặc NƠI NHẬN");
                    cb_NoiGiao_outputKG.Text = "";
                    
                    cb_NoiGiao_outputKG.Focus();
                }
                else
                {
                    LyDo1 = cb_LyDo_outputKG.Text;
                    for (i = 0; i < cb_LyDo_outputKG.Items.Count; i++)
                    {
                        if (LyDo1 == cb_LyDo_outputKG.Items[i].Text)
                        {
                            count++;
                            break;
                        }
                    }
                    if (count < 3)
                    {
                        MessageBox.Show("Kiểm tra lại lý do nhập");
                        cb_LyDo_outputKG.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void begin_load_output()
        {
            //rd_Van_outputKG.Enabled = false;
            txt_CDay_outputKG.Enabled = false;
            txt_SLVan_outputKG.Enabled = false;
            txt_SoKhoiVan_outputKG.Enabled = false;
           // rd_GoTron_outputKG.Enabled = false;
            txt_CDaiGT_outputKG.Enabled = false;
            txt_VanhGT_outputKG.Enabled = false;
            txt_SLGT_outputKG.Enabled = false;
        }
        private void print_outputKG()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Date_outputKG.Text);
                if (rd_Phach_outputKG.IsChecked == true)
                {
                    string_temp = @" select dbo.ChungTuNhap.Serial,ChungTuNhap.NoiGiao,ChungTuNhap.NoiNhan,ChungTuNhap.ADorKD,ChungTuNhap.BienBan as SoBB,ChungTuNhap.ChungTu,ChungTuNhap.LoaiGo,ChungTuNhap.LyDo,ChungTuNhap.Serial,convert(varchar(10),ChungTuNhap.Ngay,103) as Ngay
                                ,dbo.Nhap_Phach.CDai,Nhap_Phach.CDay,Nhap_Phach.CRong,Nhap_Phach.KhachHang,Nhap_Phach.SLNhap,(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,Nhap_Phach.SLXuat,(CDay*CRong*CDai*SLXuat)/1000000000 as SoKhoiXuat,Nhap_Phach.PhanLoai
                                from dbo.Nhap_Phach inner join dbo.ChungTuNhap on (dbo.Nhap_Phach.Serial_ChungTu=ChungTuNhap.Serial";
                    if (cb_QuyCach_outputKG.Text != "")
                    {
                        string_temp = prs.substring_right(cb_QuyCach_outputKG.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();

                        GhiChu1 += " and Nhap_Phach.CDay=" + DVD1 + " and Nhap_Phach.CRong=" + DVD2 + " and Nhap_Phach.CDai=" + DVD3;
                    }
                    if (cb_PhanLoai_outputKG.Text != "")
                        GhiChu1 += "and Nhap_Phach.PhanLoai='" + cb_PhanLoai_outputKG.Text + "'";
                }
                if (rd_Van_outputKG.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuNhap.Serial,ChungTuNhap.NoiGiao,ChungTuNhap.NoiNhan,ChungTuNhap.ADorKD,ChungTuNhap.BienBan as SoBB,ChungTuNhap.ChungTu,ChungTuNhap.LoaiGo,ChungTuNhap.LyDo,ChungTuNhap.Serial,convert(varchar(10),ChungTuNhap.Ngay,103) as Ngay
                                ,Nhap_Van.CDay,Nhap_Van.KhachHang,Nhap_Van.SLNhap,SoKhoiNhap,Nhap_Van.SLXuat,SoKhoiXuat
                                from dbo.Nhap_Van inner join dbo.ChungTuNhap on (dbo.Nhap_Van.Serial_ChungTu=ChungTuNhap.Serial";
                    if (txt_CDay_outputKG.Text != "")
                        GhiChu1 += " and Nhap_Van.CDay=" + txt_CDay_outputKG.Text;
                }
                if (rd_GoTron_outputKG.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuNhap.Serial,ChungTuNhap.NoiGiao,ChungTuNhap.NoiNhan,ChungTuNhap.ADorKD,ChungTuNhap.BienBan as SoBB,ChungTuNhap.ChungTu,ChungTuNhap.LoaiGo,ChungTuNhap.LyDo,ChungTuNhap.Serial,convert(varchar(10),ChungTuNhap.Ngay,103) as Ngay
                                ,dbo.Nhap_GoTron.CDai,Nhap_GoTron.Vanh,Nhap_GoTron.KhachHang,Nhap_GoTron.SLNhap,(Vanh*Vanh*CDai*SLNhap)/(4*3.141593*1000000000) as SoKhoiNhap,Nhap_GoTron.SLXuat,(Vanh*Vanh*CDai*SLXuat)/(4*3.141593*1000000000) as SoKhoiXuat
                                from dbo.Nhap_GoTron inner join dbo.ChungTuNhap on (dbo.Nhap_GoTron.Serial_ChungTu=ChungTuNhap.Serial";
                    if (txt_VanhGT_outputKG.Text != "")
                        GhiChu1 += " and Nhap_GoTron.Vanh=" + txt_VanhGT_outputKG.Text;
                    if (txt_CDaiGT_outputKG.Text != "")
                        GhiChu1 += " and Nhap_GoTron.CDai=" + txt_CDaiGT_outputKG.Text;
                }
                if (cb_NoiGiao_outputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.NoiGiao='" + prs.substring_left(cb_NoiGiao_outputKG.Text, child) + "'";
                if (cb_NoiNhan_outputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.NoiNhan='" + prs.substring_left(cb_NoiNhan_outputKG.Text, child) + "'";
                if (cb_NguonGo_outputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.LoaiGo='" + cb_NguonGo_outputKG.Text + "'";
                if (cb_LyDo_outputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.LyDo='" + prs.substring_right(cb_LyDo_outputKG.Text, child) + "'";
                if (txt_ChungTu_outputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.ChungTu='" + txt_ChungTu_outputKG.Text + "'";
                if (txt_BienBan_outputKG.Text != "")
                    GhiChu1 += " and ChungTuNhap.BienBan='" + txt_BienBan_outputKG.Text + "'";
                if (cb_AdorKD_outputKG.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_outputKG.Text + "'";
                string_temp += GhiChu1 + " and SLXuat<>0 and ChungTuNhap.Ngay='" + Ngay1 + "')";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Date_outputKG.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);

                if (rd_Phach_outputKG.IsChecked == true || rd_Van_outputKG.IsChecked == true)
                {
                    FrR.CrR_XK_PhachSC(ds, Ngay1);
                }
                if (rd_GoTron_outputKG.IsChecked == true)
                    FrR.CrR_XK_GoTron(ds, Ngay1);

                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }
        private void print_TH_outputKG()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Date_outputKG.Text);

                string_temp = @" SELECT [NoiGiao],[NoiNhan],[ADorKD],[LoaiGo] ,[LyDo] ,[BienBan] as SoBB,[ChungTu],[Ngay] ,[DVD1],[DVD2],[DVD3],[PhanLoai],[SLXuat] ,[SoKhoiXuat]
                                     ,[KhachHang],[GhiChu],[Serial_ChungTu] as Serial
                                    FROM [Planning].[dbo].[NhapGo] where SLXuat<>0 ";

                if (cb_NoiGiao_outputKG.Text != "")
                    GhiChu1 += " and NoiGiao='" + prs.substring_left(cb_NoiGiao_outputKG.Text, child) + "'";
                if (cb_NoiNhan_outputKG.Text != "")
                    GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_outputKG.Text, child) + "'";
                if (cb_NguonGo_outputKG.Text != "")
                    GhiChu1 += " and LoaiGo='" + cb_NguonGo_outputKG.Text + "'";
                if (cb_LyDo_outputKG.Text != "")
                    GhiChu1 += " and LyDo='" + prs.substring_right(cb_LyDo_outputKG.Text, child) + "'";
                if (txt_ChungTu_outputKG.Text != "")
                    GhiChu1 += " and ChungTu='" + txt_ChungTu_outputKG.Text + "'";
                if (txt_BienBan_outputKG.Text != "")
                    GhiChu1 += " and BienBan='" + txt_BienBan_outputKG.Text + "'";
                if (cb_AdorKD_outputKG.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_outputKG.Text + "'";
                string_temp += GhiChu1 + " and Ngay='" + Ngay1 + "'";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Date_outputKG.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);


                FrR.CrR_KG_XuatKho(ds, Ngay1);


                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }


        private void Addgrid_output()
        {
            
            try
            {

                DVT1 = cb_DVT_outputKG.Text.Trim();
                KhachHang1 = cb_KhachHang_outputKG.Text.Trim();
                GhiChu1 = txt_GhiChu_outputKG.Text;
                count=0;

                /*NoiGiao1 = prs.substring_left(cb_NoiGiao_outputKG.Text, child);
                NoiNhan1 = prs.substring_left(cb_NoiNhan_outputKG.Text, child);
                ADorKD1 = cb_AdorKD_outputKG.Text.Trim();
                PhanLoai1 = cb_PhanLoai_outputKG.Text.Trim();
                LyDo1 = prs.substring_right(cb_LoaiGo_outputKG.Text, child);
                BienBan1 = txt_BienBan_outputKG.Text.Trim();
                ChungTu1 = txt_BienBan_outputKG.Text.Trim();
                Ngay1 = Date_Split(dtp_Date_outputKG.Text);
                SoXe1 = txt_SoXe_outputKG.Text.Trim();
                ChungTu1 = txt_ChungTu_outputKG.Text.Trim();
                */
                if (rd_Phach_outputKG.IsChecked == true)
                {
                    txt_SLPhach_outputKG_Leave(null, null);
                    for (i = 0; i < cb_QuyCach_outputKG.Items.Count; i++)
                    {
                        if (cb_QuyCach_outputKG.Text == cb_QuyCach_outputKG.Items[i].ToString())
                        {
                            count++;
                            break;
                        }
                    }
                    if (count == 0)
                        MessageBox.Show("Nhập sai quy cách");
                    else
                    {
                        
                        string_temp = prs.substring_right(cb_QuyCach_outputKG.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                        PhanLoai1 = cb_PhanLoai_outputKG.Text.Trim();
                        SoLuong1 = txt_SLPhach_outputKG.Text.Trim();
                        temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                        SoKhoi1 = temp_double.ToString();
                        
                    }
                    

                }
                if (rd_Van_outputKG.IsChecked == true)
                {
                    txt_SLVan_outputKG_Leave(null, null);
                    DVD1 = txt_CDay_outputKG.Text.Trim();
                    SoLuong1 = txt_SLVan_outputKG.Text.Trim();
                    SoKhoi1 = txt_SoKhoiVan_outputKG.Text.Trim();
                    if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        count = 1;
                        DVD2 = "";
                        DVD3 = "";
                    }
                    
                }
                if (rd_GoTron_outputKG.IsChecked == true)
                {
                    //txt_SLGT_outputKG_Leave(null, null);
                    DVD2 = txt_CDaiGT_outputKG.Text.Trim();
                    DVD1 = txt_VanhGT_outputKG.Text.Trim();
                    SoLuong1 = txt_SLGT_outputKG.Text.Trim();

                    if (DVD1 == "" || DVD2 == "" || SoLuong1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        Conn = prs.Connect();
                        Conn.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = Conn;
                        cmd.CommandText = @"select sum(SoKhoiTon) from dbo.KhoGo_TK where Nhom='GoTron'";
                        //MessageBox.Show(cmd.CommandText.ToString());
                        lb_TonKho_outputKG.Text = cmd.ExecuteScalar().ToString();
                        temp_double = double.Parse(DVD2) * double.Parse(DVD1) * double.Parse(DVD1) *double.Parse(SoLuong1)/ (4 * 3.1415926 * 1000000000);
                        if (temp_double > double.Parse(lb_TonKho_outputKG.Text))
                        {
                            MessageBox.Show("Số khối hiện tại đã vượt quá số khối tồn kho");
                        }
                        SoKhoi1 = temp_double.ToString();
                        count = 1;
                        DVD3 = "";
                    }
                }
                if (count == 1)
                {
                    if (chk_ChoXuLy.Checked == true && chk_ChoXuLy.Enabled == true)
                        Wait1 = "1";
                    else
                        Wait1 = "0";
                    int_temp = radGridView2.Rows.Count;
                    if (int_temp > 0)
                    {
                        i = int.Parse(radGridView2.Rows[int_temp - 1].Cells["STT_outputKG"].Value.ToString()) + 1;
                        radGridView2.Rows.Add((i).ToString(),DVD1, DVD2, DVD3, SoLuong1, SoKhoi1, PhanLoai1, DVT1, KhachHang1, GhiChu1, Wait1, "", "");
                    }
                    else
                        radGridView2.Rows.Add("1", DVD1, DVD2, DVD3, SoLuong1, SoKhoi1, PhanLoai1, DVT1, KhachHang1, GhiChu1, Wait1, "", "");
                    //temp_double=double.Parse(
                    cb_QuyCach_outputKG.Text = "";
                    cb_PhanLoai_outputKG.Text = "";
                    txt_SLPhach_outputKG.Text = "";
                    txt_CDay_outputKG.Text = "";
                    txt_SLVan_outputKG.Text = "";
                    txt_SoKhoiVan_outputKG.Text = "";
                    txt_CDaiGT_outputKG.Text = "";
                    txt_VanhGT_outputKG.Text = "";
                    txt_SLGT_outputKG.Text = "";
                    lb_TonKho_outputKG.Text = "0";
                    txt_QC1_outputKG.Text = "";
                    txt_QC2_outputKG.Text = "";
                    txt_QC3_outputKG.Text = "";
                    if (cb_NoiGiao_outputKG.Enabled == true)
                    {
                        check_main_infooutput();
                        //main_info_Disableoutput();
                        if (rd_Phach_outputKG.IsChecked == true)
                        {
                            rd_GoTron_outputKG.Enabled = false;
                            rd_Van_outputKG.Enabled = false;
                            txt_CDay_outputKG.Enabled = false;
                            txt_SLVan_outputKG.Enabled = false;
                            txt_SoKhoiVan_outputKG.Enabled = false;
                            txt_CDaiGT_outputKG.Enabled = false;
                            txt_VanhGT_outputKG.Enabled = false;
                            txt_SLGT_outputKG.Enabled = false;
                            txt_QC1_outputKG.Focus();
                            
                        }
                        if (rd_Van_outputKG.IsChecked == true)
                        {
                            rd_GoTron_outputKG.Enabled = false;
                            rd_Phach_outputKG.Enabled = false;
                            txt_CDaiGT_outputKG.Enabled = false;
                            txt_VanhGT_outputKG.Enabled = false;
                            txt_SLGT_outputKG.Enabled = false;
                            cb_QuyCach_outputKG.Enabled = false;
                            txt_SLPhach_outputKG.Enabled = false;
                            cb_PhanLoai_outputKG.Enabled = false;
                            txt_CDay_outputKG.Focus();
                        }
                        if (rd_GoTron_outputKG.IsChecked == true)
                        {
                            rd_Van_outputKG.Enabled = false;
                            rd_Phach_outputKG.Enabled = false;
                            cb_QuyCach_outputKG.Enabled = false;
                            txt_SLPhach_outputKG.Enabled = false;
                            cb_PhanLoai_outputKG.Enabled = false;
                            txt_CDay_outputKG.Enabled = false;
                            txt_SLVan_outputKG.Enabled = false;
                            txt_SoKhoiVan_outputKG.Enabled = false;
                            txt_VanhGT_outputKG.Focus();
                        }
                    }
                    if (rd_Phach_outputKG.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_outputKG.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_outputKG.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_outputKG.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_outputKG.Text = string.Format("{0:###,###,###}", temp_double);
                        //cb_QuyCach_outputKG.Focus();
                        txt_QC1_outputKG.Focus();
                    }
                    if (rd_Van_outputKG.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_outputKG.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_outputKG.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_outputKG.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_outputKG.Text = string.Format("{0:###,###,###.#}", temp_double);
                        txt_CDay_outputKG.Focus();
                    }
                    if (rd_GoTron_outputKG.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_outputKG.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_outputKG.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_outputKG.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_outputKG.Text = temp_double.ToString();
                        txt_SLGT_outputKG.Text = "1";
                        txt_VanhGT_outputKG.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
        private void dtp_Date_outputKG_Leave(object sender, EventArgs e)
        {
            
        }
        private void bt_add_outputKG_Click(object sender, EventArgs e)
        {
            Addgrid_output();
        }

        private void radGridView2_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView2.Rows.Count > 0)
            {
                try
                {
                    //
                    int r = radGridView2.CurrentCell.RowIndex;
                    temp_double = double.Parse(lb_TongSL_outputKG.Text) - double.Parse(radGridView2.Rows[r].Cells["SoLuong_outputKG"].Value.ToString());
                    lb_TongSL_outputKG.Text = string.Format("{0:###,###}", temp_double);
                    temp_double = double.Parse(lb_TongKhoi_outputKG.Text) - double.Parse(radGridView2.Rows[r].Cells["SoKhoi_outputKG"].Value.ToString());
                    lb_TongKhoi_outputKG.Text = string.Format("{0:###,###.#####}", temp_double);
                    radGridView2.Rows.RemoveAt(r);
                    j = radGridView2.Rows.Count - 1;
                 

                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (radGridView2.Rows.Count == 0)
            {
                lb_TongKhoi_outputKG.Text = "0";
                lb_TongSL_outputKG.Text = "0";
            }
        }

        private void cb_LyDo_outputKG_Leave(object sender, EventArgs e)
        {
            /*check_main_infooutput();*/
        }
        private void save_output()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Tran_Ins_CTNhap";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {

                
                //string insert_NhapKTam = null;
                if (lb_Serial_addoutput.Text == "0"&& (cb_NoiGiao_outputKG.Text == "" || cb_NoiNhan_outputKG.Text == "" || cb_AdorKD_outputKG.Text == "" || cb_NguonGo_outputKG.Text == "" || cb_LyDo_outputKG.Text == "" || txt_BienBan_outputKG.Text == "" || txt_ChungTu_outputKG.Text == ""||radGridView2.Rows.Count==0))
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    if (lb_Serial_addoutput.Text == "0" || lb_Serial_addoutput.Visible == false)
                    {
                        NoiGiao1 = prs.substring_left(cb_NoiGiao_outputKG.Text, child);
                        NoiNhan1 = prs.substring_left(cb_NoiNhan_outputKG.Text, child);
                        ADorKD1 = cb_AdorKD_outputKG.Text.Trim();
                        LoaiGo1 = cb_NguonGo_outputKG.Text.Trim();
                        LyDo1 = prs.substring_right(cb_LyDo_outputKG.Text, child);
                        BienBan1 = txt_BienBan_outputKG.Text.Trim();
                        ChungTu1 = txt_ChungTu_outputKG.Text.Trim();
                        SoXe1 = txt_SoXe_outputKG.Text.Trim();
                        Ngay1 = Date_Split(dtp_Date_outputKG.Text);

                        cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                        cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                        cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                        cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                        cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                        cmd.Parameters.AddWithValue("@SoXe", SoXe1);
                        cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                        cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                        SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                        code.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(code);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                        sdr.Close();
                        cmd.Parameters.Clear();
                    }
                    else
                        Serial1 = lb_Serial_addoutput.Text;
                    //MessageBox.Show("aaaa");
                    if (rd_Phach_outputKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapPhach";
                        i = 0;
                        while (radGridView2.Rows.Count>0)
                        {
                            DVD1 = radGridView2.Rows[i].Cells["DVD1_outputKG"].Value.ToString();
                            DVD2 = radGridView2.Rows[i].Cells["DVD2_outputKG"].Value.ToString();
                            DVD3 = radGridView2.Rows[i].Cells["DVD3_outputKG"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView2.Rows[i].Cells["SoLuong_outputKG"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView2.Rows[i].Cells["SoKhoi_outputKG"].Value.ToString();
                            PhanLoai1 = radGridView2.Rows[i].Cells["PhanLoai_outputKG"].Value.ToString();
                            DVT1 = radGridView2.Rows[i].Cells["DVT_outputKG"].Value.ToString();
                            KhachHang1 = radGridView2.Rows[i].Cells["KhachHang_outputKG"].Value.ToString();
                            GhiChu1 = radGridView2.Rows[i].Cells["GhiChu_outputKG"].Value.ToString();
                            Wait1 = radGridView2.Rows[i].Cells["Wait_outputKG"].Value.ToString();
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                           
                            cmd.Parameters.AddWithValue("@Wait", Wait1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView2.Rows.RemoveAt(i);
                        }
                    }

                    if (rd_Van_outputKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapVan";
                        i = 0;
                        while (radGridView2.Rows.Count > 0)
                        {
                            DVD1 = radGridView2.Rows[i].Cells["DVD1_outputKG"].Value.ToString();
                            // DVD2 = radGridView2.Rows[i].Cells["DVT2_outputKG"].Value.ToString();
                            //DVD3 = radGridView2.Rows[i].Cells["DVT3_outputKG"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView2.Rows[i].Cells["SoLuong_outputKG"].Value.ToString()).ToString();
                            SoKhoi1 = radGridView2.Rows[i].Cells["SoKhoi_outputKG"].Value.ToString();
                            //PhanLoai1 = radGridView2.Rows[i].Cells["PhanLoai_outputKG"].Value.ToString();
                            DVT1 = radGridView2.Rows[i].Cells["DVT_outputKG"].Value.ToString();
                            KhachHang1 = radGridView2.Rows[i].Cells["KhachHang_outputKG"].Value.ToString();
                            GhiChu1 = radGridView2.Rows[i].Cells["GhiChu_outputKG"].Value.ToString();
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", SoKhoi1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            
                            cmd.Parameters.AddWithValue("@Wait", Wait1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView2.Rows.RemoveAt(i);
                        }


                    }
                    if (rd_GoTron_outputKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapGT";
                        i = 0;
                        while (radGridView2.Rows.Count > 0)
                        {
                            DVD1 = double.Parse(radGridView2.Rows[i].Cells["DVD1_outputKG"].Value.ToString()).ToString();
                            DVD2 = double.Parse(radGridView2.Rows[i].Cells["DVD2_outputKG"].Value.ToString()).ToString();
                            //DVD3 = radGridView2.Rows[i].Cells["DVT3_outputKG"].Value.ToString();
                            SoLuong1 = SoLuong1 = double.Parse(radGridView2.Rows[i].Cells["SoLuong_outputKG"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView2.Rows[i].Cells["SoKhoi_outputKG"].Value.ToString();
                            //PhanLoai1 = radGridView2.Rows[i].Cells["PhanLoai_outputKG"].Value.ToString();
                            DVT1 = radGridView2.Rows[i].Cells["DVT_outputKG"].Value.ToString();
                            KhachHang1 = radGridView2.Rows[i].Cells["KhachHang_outputKG"].Value.ToString();
                            GhiChu1 = radGridView2.Rows[i].Cells["GhiChu_outputKG"].Value.ToString();


                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            
                            cmd.Parameters.AddWithValue("@Wait", Wait1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView2.Rows.RemoveAt(i);
                        }
                    }
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    bt_rework_outputKG_Click(null, null);
                }
            }

            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
           
        }

        private void txt_CDay_outputKG_Leave(object sender, EventArgs e)
        {
            if (txt_CDay_outputKG.Text != "")
            {
                prs.checktext_number(txt_CDay_outputKG);
                Conn = prs.Connect();
                SqlCommand cmd = new SqlCommand();
               
                try
                {
                    Conn.Open();
                    cmd.Connection = Conn;
                    cmd.CommandText = @"select SoKhoiTon from dbo.KhoGo_TK where DVD1="+txt_CDay_outputKG.Text+" and DVD2=0 and ADorKD='"+cb_AdorKD_outputKG.Text+"'";
                    lb_TonKho_outputKG.Text = cmd.ExecuteScalar().ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    lb_TonKho_outputKG.Text = "0";
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        /*private void radGridView2_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView2);
        }*/
        private void txt_QC3_outputKG_Leave(object sender, EventArgs e)
        {
            try
            {

                if (txt_QC3_outputKG.Text != "")
                {
                    Conn = prs.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;
                    string_temp = txt_QC1_outputKG.Text.Trim() + txt_QC2_outputKG.Text.Trim() + txt_QC3_outputKG.Text.Trim();
                    if (txt_QC3_outputKG.Text != "")
                    {
                        string_temp = txt_QC1_outputKG.Text.Trim() + txt_QC2_outputKG.Text.Trim() + txt_QC3_outputKG.Text.Trim();
                        for (i = 0; i < dataGridView_QuyCach.Rows.Count - 1; i++)
                        {
                            if (string_temp == dataGridView_QuyCach.Rows[i].Cells["QuyCach"].Value.ToString())
                            {
                                cb_PhanLoai_outputKG.Text = dataGridView_QuyCach.Rows[i].Cells["PhanLoai"].Value.ToString();
                                cb_QuyCach_outputKG.Text = dataGridView_QuyCach.Rows[i].Cells["QuyCach"].Value.ToString() + " - " + dataGridView_QuyCach.Rows[i].Cells["size"].Value.ToString();
                                //MessageBox.Show(cb_QuyCach_outputKG.Text, "aaa");
                                break;
                            }
                        }
                       
                           
                           
                        
                       // MessageBox.Show(i.ToString());
                    }
                    if (i == dataGridView_QuyCach.Rows.Count - 1)
                    {
                        MessageBox.Show("Bạn đang nhập sai quy cách");
                        cb_QuyCach_outputKG.Text = "";
                    }
                    else
                    {
                        DVD1 = txt_QC1_outputKG.Text.Trim();
                        DVD2 = txt_QC2_outputKG.Text.Trim();
                        DVD3 = txt_QC3_outputKG.Text.Trim();
                        //temp.Clone();
                        cmd.CommandText = "select KhoGo_TK.SoKhoiTon from dbo.KhoGo_TK where DVD1=" + DVD1 + " and DVD2=" + DVD2 + " and DVD3=" + DVD3 + " and ADorKD='" + cb_AdorKD_outputKG.Text + "'";
                        //MessageBox.Show(cmd.CommandText.ToString());
                        lb_TonKho_outputKG.Text = cmd.ExecuteScalar().ToString();
                    }
                    Conn.Close();
                    cmd.Dispose();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void txt_SLPhach_outputKG_Leave(object sender, EventArgs e)
        {
            if(txt_SLPhach_outputKG.Text!="")
            {
                try
                {
                    prs.checktext_number(txt_SLPhach_outputKG);
                    if (chk_ChoXuLy.Checked == false || chk_ChoXuLy.Enabled == false)
                    {
                        string_temp = prs.substring_right(cb_QuyCach_outputKG.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                        SoKhoi_d = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(txt_SLPhach_outputKG.Text) / 1000000000;
                        if (SoKhoi_d > double.Parse(lb_TonKho_outputKG.Text))
                        {
                            
                            MessageBox.Show("Số khối đã vượt quá số khối tồn kho, số lượng cho phép <" + string.Format("{0:###,###,###}", (double.Parse(lb_TonKho_outputKG.Text) * 1000000000 / (double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3)))));
                            //txt_SLPhach_outputKG.Text = "";
                            txt_SLPhach_outputKG.Focus();
                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }

        private void txt_SLVan_outputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLVan_outputKG);
        }

        private void txt_SoKhoiVan_outputKG_Leave(object sender, EventArgs e)
        {
            if (txt_SoKhoiVan_outputKG.Text != "")
            {
                prs.checktext_number(txt_SoKhoiVan_outputKG);
                if (chk_ChoXuLy.Checked == false || chk_ChoXuLy.Enabled == false)
                {
                    if (double.Parse(txt_SoKhoiVan_outputKG.Text) > double.Parse(lb_TonKho_outputKG.Text))
                    {
                        MessageBox.Show("Số khối nhập đã vượt quá số khối tồn kho");
                        //txt_SoKhoiVan_outputKG.Text = "";
                        txt_SoKhoiVan_outputKG.Focus();
                    }
                }
            }
        }

        private void txt_CDaiGT_outputKG_Leave(object sender, EventArgs e)
        {
            if (txt_CDaiGT_outputKG.Text != "")
            {
                prs.checktext_number(txt_CDaiGT_outputKG);
                if (txt_VanhGT_outputKG.Text=="")
                {
                    MessageBox.Show("Bạn cần nhập vanh");
                }
                else
                {

                    //txt_SLGT_outputKG.Focus();
                    Conn = prs.Connect();
                    SqlCommand cmd = new SqlCommand();

                    try
                    {
                        Conn.Open();
                        cmd.Connection = Conn;
                        cmd.CommandText = @"select sum(SoKhoiTon) from dbo.KhoGo_TK where Nhom='GoTron'";
                        //MessageBox.Show(cmd.CommandText.ToString());
                        lb_TonKho_outputKG.Text = cmd.ExecuteScalar().ToString();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        lb_TonKho_outputKG.Text = "0";
                    }
                    finally
                    {
                        Conn.Close();
                        cmd.Dispose();
                    }
                }
            }
           
        }

        private void txt_VanhGT_outputKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_VanhGT_outputKG);
           
        }

        private void txt_SLGT_outputKG_Leave(object sender, EventArgs e)
        {
            if (txt_SLGT_outputKG.Text != "")
            {
                prs.checktext_number(txt_SLGT_outputKG);
                if (chk_ChoXuLy.Enabled == false || chk_ChoXuLy.Checked == false)
                {
                    try
                    {
                        if (txt_CDaiGT_outputKG.Text != "" && txt_VanhGT_outputKG.Text != "")
                        {
                            temp_double = double.Parse(txt_VanhGT_outputKG.Text);
                            SoLuong_d = double.Parse(txt_SLGT_outputKG.Text);
                            
                            SoKhoi_d = temp_double * temp_double * SoLuong_d*double.Parse(txt_CDaiGT_outputKG.Text) / (4 * 3.1415926 * 1000000000);
                            if (SoLuong_d > double.Parse(lb_TonKho_outputKG.Text))
                            {
                                MessageBox.Show("Số khối xuất vượt quá số khối tồn kho");
                                //txt_SLGT_outputKG.Text = "";
                                txt_SLGT_outputKG.Focus();
                            }
                        }


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                
            }
        }
        private void bt_save_outputKG_Click(object sender, EventArgs e)
        {
            save_output();
        }

        private void cb_AdorKD_outputKG_Leave(object sender, EventArgs e)
        {
            if (cb_AdorKD_outputKG.Text != "AD" && cb_AdorKD_outputKG.Text != "KD")
            {
                MessageBox.Show("Nhập sai thông tin");
                cb_AdorKD_outputKG.Text = "";
            }
        }

        private void bt_rework_outputKG_Click(object sender, EventArgs e)
        {
            main_info_Enableoutput();
            prs.reset_page_txt(Page_Xuat_KGo);
            cb_NoiGiao_outputKG.Focus();
            if (radGridView2.Rows.Count > 0)
                radGridView2.Rows.Clear();
            rd_GoTron_outputKG.Enabled = true;
            rd_Phach_outputKG.Enabled = true;
            rd_Van_outputKG.Enabled = true;
            lb_Serial_output.Text = "0";
            lb_TongKhoi_outputKG.Text = "0";
            lb_TongSL_outputKG.Text = "0";
            chk_ChoXuLy.Checked = false;
            chk_ChoXuLy.Enabled = false;
            lb_Serial_addoutput.Text = "0";
            lb_Serial_addoutput.Visible = false;
            lb_CapNhat_output.Visible = false;
            bt_edit_outputKG.Enabled = false;
            bt_delete_outputKG.Enabled = false;
            bt_save_outputKG.Enabled = true;
        }
        private void Display_output(int row)
        {
            //cb_NoiGiao_outputKG.Text = radGridView2.Rows[i].Cells["NoiGiao"].Value.ToString();

            if (radGridView2.Rows[row].Cells["Nhom_output"].Value.ToString() != "" && radGridView2.Rows[row].Cells["SoLuong_outputKG"].Value.ToString()!="0")
                {

                    if (radGridView2.Rows[row].Cells["Nhom_output"].Value.ToString() == "Phach")
                    {
                        rd_Phach_outputKG.Enabled = true;
                        rd_Phach_outputKG.IsChecked = true;
                        rd_Van_outputKG.Enabled = false;
                        rd_GoTron_outputKG.Enabled = false;
                        cb_QuyCach_outputKG.Text = radGridView2.Rows[row].Cells["DVD1_outputKG"].Value.ToString() + radGridView2.Rows[row].Cells["DVD2_outputKG"].Value.ToString() + radGridView2.Rows[row].Cells["DVD3_outputKG"].Value.ToString() + " - " + radGridView2.Rows[row].Cells["DVD1_outputKG"].Value.ToString() +"x"+ radGridView2.Rows[row].Cells["DVD2_outputKG"].Value.ToString() +"x"+ radGridView2.Rows[row].Cells["DVD3_outputKG"].Value.ToString();
                        cb_PhanLoai_outputKG.Text = radGridView2.Rows[row].Cells["PhanLoai_outputKG"].Value.ToString();
                        txt_SLPhach_outputKG.Text = radGridView2.Rows[row].Cells["SoLuong_outputKG"].Value.ToString();
                    }
                    if (radGridView2.Rows[row].Cells["Nhom_output"].Value.ToString() == "Van")
                    {
                        rd_Van_outputKG.Enabled = true;
                        rd_Van_outputKG.IsChecked = true;
                        rd_Phach_outputKG.Enabled = false;
                        rd_GoTron_outputKG.Enabled = false;
                        txt_CDay_outputKG.Text = radGridView2.Rows[row].Cells["DVD1_outputKG"].Value.ToString();
                        txt_SLVan_outputKG.Text = radGridView2.Rows[row].Cells["SoLuong_outputKG"].Value.ToString();
                        txt_SoKhoiVan_outputKG.Text = radGridView2.Rows[row].Cells["SoKhoi_outputKG"].Value.ToString();
                    }
                    if (radGridView2.Rows[row].Cells["Nhom_output"].Value.ToString() == "GoTron")
                    {
                        rd_GoTron_outputKG.Enabled = true;
                        rd_GoTron_outputKG.IsChecked = true;
                        rd_Phach_outputKG.Enabled = false;
                        rd_Van_outputKG.Enabled = false;
                        txt_CDaiGT_outputKG.Text = radGridView2.Rows[row].Cells["DVD2_outputKG"].Value.ToString();
                        txt_VanhGT_outputKG.Text = radGridView2.Rows[row].Cells["DVD1_outputKG"].Value.ToString();
                        txt_SLGT_outputKG.Text = radGridView2.Rows[row].Cells["SoLuong_outputKG"].Value.ToString();
                    }
                    cb_DVT_outputKG.Text = radGridView2.Rows[row].Cells["DVT_outputKG"].Value.ToString();
                    cb_KhachHang_outputKG.Text = radGridView2.Rows[row].Cells["KhachHang_outputKG"].Value.ToString();
                    lb_Serial_output.Text = radGridView2.Rows[row].Cells["Serial_outputKG"].Value.ToString();
                    lb_index.Text = row.ToString();
                }
           
        }
        private void search_outputKG()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = "prc_Search_KGinput";
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                lb_Serial_output.Text = "0";
                
                Serial1 = txt_Serial_output.Text;
                cmd.Parameters.AddWithValue("@Serial", Serial1);
                if (radGridView2.Rows.Count > 0)
                    radGridView2.Rows.Clear();
                DataSet ds = new DataSet();
                ds=prs.dts_sql(cmd, Conn, ds);
                prs.reset_page_txt(Page_Xuat_KGo);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    cb_NoiGiao_outputKG.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                    cb_NoiNhan_outputKG.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                    cb_AdorKD_outputKG.Text = ds.Tables[0].Rows[0]["ADorKD"].ToString();
                    cb_NguonGo_outputKG.Text = ds.Tables[0].Rows[0]["LoaiGo"].ToString();
                    cb_LyDo_outputKG.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                    txt_BienBan_outputKG.Text = ds.Tables[0].Rows[0]["BienBan"].ToString();
                    txt_ChungTu_outputKG.Text = ds.Tables[0].Rows[0]["ChungTu"].ToString();
                    txt_SoXe_outputKG.Text = ds.Tables[0].Rows[0]["SoXe"].ToString();
                    dtp_Date_outputKG.Text = ds.Tables[0].Rows[0]["Ngay"].ToString();
                    main_info_Disableoutput();
                    temp_double = 0;
                    SoKhoi_d = 0;
                   
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        radGridView2.Rows.Add((i+1).ToString(),ds.Tables[0].Rows[i]["DVD1"].ToString(), ds.Tables[0].Rows[i]["DVD2"].ToString(), ds.Tables[0].Rows[i]["DVD3"].ToString(), ds.Tables[0].Rows[i]["SLXuat"].ToString(), ds.Tables[0].Rows[i]["SoKhoiXuat"].ToString(), ds.Tables[0].Rows[i]["PhanLoai"].ToString(), ds.Tables[0].Rows[i]["DVT"].ToString(), ds.Tables[0].Rows[i]["KhachHang"].ToString(), ds.Tables[0].Rows[i]["GhiChu"].ToString(), ds.Tables[0].Rows[i]["Wait"].ToString(), ds.Tables[0].Rows[i]["Nhom"].ToString(), ds.Tables[0].Rows[i]["ID"].ToString());
                        temp_double += double.Parse(ds.Tables[0].Rows[i]["SLXuat"].ToString());
                        SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiXuat"].ToString());
                    }
                    if (SoKhoi_d == 0)
                        lb_TongKhoi_outputKG.Text = "0";
                    else
                        lb_TongKhoi_outputKG.Text = string.Format("{0:###,###.##}", SoKhoi_d);
                    if (temp_double == 0)
                        lb_TongSL_outputKG.Text = "0";
                    
                    else
                        lb_TongSL_outputKG.Text = string.Format("{0:###,###}", temp_double);
                   
                }
                else
                    MessageBox.Show("Không có dữ liệu vui lòng kiểm tra lại thông tin tìm kiếm");
                ds.Clear();
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("Kiểm tra lại Serial " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_Search_outputKG_Click(object sender, EventArgs e)
        {
            search_outputKG();
            bt_edit_outputKG.Enabled = true;
            bt_delete_outputKG.Enabled = true;
            bt_save_outputKG.Enabled = false;
        }

        private void radGridView2_Click_1(object sender, EventArgs e)
        {
            try
            {
                if (radGridView2.Rows.Count > 0)
                {

                    int k = radGridView2.CurrentRow.Index;
                    //MessageBox.Show(k.ToString());
                    Display_output(k);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bt_Prev_outputKG_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView2.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_output(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_outputKG_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView2.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i < radGridView2.Rows.Count - 1)
                    {
                        i++;
                        Display_output(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void edit_output()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            DVT1 = cb_DVT_outputKG.Text.Trim();
            KhachHang1 = cb_KhachHang_outputKG.Text.Trim();
            GhiChu1 = txt_GhiChu_outputKG.Text;
            count = 0;
            cmd.CommandType = CommandType.StoredProcedure;
            Serial1 = lb_Serial_output.Text;
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (TL == DialogResult.Yes)
            {
                try
                {
                    if (rd_Phach_outputKG.IsChecked == true)
                    {
                        for (i = 0; i < cb_QuyCach_outputKG.Items.Count; i++)
                        {
                            if (cb_QuyCach_outputKG.Text == cb_QuyCach_outputKG.Items[i].ToString())
                            {
                                count++;
                                break;
                            }
                        }
                        if (count == 0)
                            MessageBox.Show("Nhập sai quy cách");
                        else
                        {
                            count = 0;
                            string_temp = prs.substring_right(cb_QuyCach_outputKG.Text, child);
                            string[] temp = string_temp.Split('x');
                            DVD1 = temp[0].ToString().Trim();
                            DVD2 = temp[1].ToString().Trim();
                            DVD3 = temp[2].ToString().Trim();
                            temp.Clone();
                            PhanLoai1 = cb_PhanLoai_outputKG.Text.Trim();
                            SoLuong1 = double.Parse(txt_SLPhach_outputKG.Text.Trim()).ToString();
                            //temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                            //SoKhoi1 = temp_double.ToString();
                            cmd.CommandText = "UpdNhapPhach";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            count = 1;
                        }


                    }
                    if (rd_Van_outputKG.IsChecked == true)
                    {

                        DVD1 = txt_CDay_outputKG.Text.Trim();
                        SoLuong1 = double.Parse(txt_SLVan_outputKG.Text.Trim()).ToString();
                        SoKhoi1 = txt_SoKhoiVan_outputKG.Text.Trim();
                        if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            cmd.CommandText = "UpdNhapVan";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", SoKhoi1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            count = 1;

                        }

                    }
                    if (rd_GoTron_outputKG.IsChecked == true)
                    {
                        DVD2 = double.Parse(txt_CDaiGT_outputKG.Text.Trim()).ToString();
                        DVD1 = double.Parse(txt_VanhGT_outputKG.Text.Trim()).ToString();
                        SoLuong1 = double.Parse(txt_SLGT_outputKG.Text.Trim()).ToString();

                        if (DVD1 == "" || DVD2 == "" || SoLuong1 == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            cmd.CommandText = "UpdNhapGT";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            //temp_double = double.Parse(DVD2) * double.Parse(DVD2) * double.Parse(DVD1) / (4 * 3.1415926 * 1000000000);
                            //SoKhoi1 = temp_double.ToString();
                            count = 1;
                        }
                    }
                    if (count == 1)
                    {
                        MessageBox.Show("Chỉnh sửa thành công");
                        bt_rework_outputKG_Click(null, null);
                    }

                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void bt_edit_outputKG_Click(object sender, EventArgs e)
        {
            edit_output();
        }
        private void delete_output()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                DialogResult TL;
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA ID: " + lb_Serial_output.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (TL == DialogResult.Yes)
                {
                    if (rd_Phach_outputKG.IsChecked == true)
                    {
                        string_temp = @"DELETE FROM [dbo].[Nhap_Phach] WHERE Serial=" + lb_Serial_output.Text;
                    }
                    if (rd_Van_outputKG.IsChecked == true)
                    {
                        string_temp = @"DELETE FROM [dbo].[Nhap_Van] WHERE Serial=" + lb_Serial_output.Text;
                    }
                    if (rd_GoTron_outputKG.IsChecked == true)
                    {
                        string_temp = @"DELETE FROM [dbo].[Nhap_GoTron] WHERE Serial=" + lb_Serial_output.Text;
                    }
                    cmd.CommandText = string_temp;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa thành công");
                    bt_rework_outputKG_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("ĐÃ XẢY RA LỖI. DỮ LIỆU XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_delete_outputKG_Click(object sender, EventArgs e)
        {
            delete_output();
        }
        private void check_TonKho()
        {
            if (rd_Phach_outputKG.IsChecked == true)
            {
                for (i = 0; i < cb_QuyCach_outputKG.Items.Count; i++)
                {
                    if (cb_QuyCach_outputKG.Text == cb_QuyCach_outputKG.Items[i].ToString())
                    {
                        count++;
                        break;
                    }
                }
                if (count == 0)
                    MessageBox.Show("Nhập sai quy cách");
                else
                {

                    string_temp = prs.substring_right(cb_QuyCach_outputKG.Text, child);
                    string[] temp = string_temp.Split('x');
                    DVD1 = temp[0].ToString().Trim();
                    DVD2 = temp[1].ToString().Trim();
                    DVD3 = temp[2].ToString().Trim();
                    temp.Clone();
                    PhanLoai1 = cb_PhanLoai_outputKG.Text.Trim();
                    SoLuong1 = txt_SLPhach_outputKG.Text.Trim();
                    temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                    SoKhoi_d = temp_double;

                }


            }
            if (rd_Van_outputKG.IsChecked == true)
            {

                DVD1 = txt_CDay_outputKG.Text.Trim();
                SoLuong1 = txt_SLVan_outputKG.Text.Trim();
                SoKhoi_d = double.Parse(txt_SoKhoiVan_outputKG.Text.Trim());

                if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    count = 1;
                }

            }
            if (rd_GoTron_outputKG.IsChecked == true)
            {
                DVD2 = txt_CDaiGT_outputKG.Text.Trim();
                DVD1 = txt_VanhGT_outputKG.Text.Trim();

                SoLuong1 = txt_SLGT_outputKG.Text.Trim();

                if (DVD3 == "" || DVD2 == "" || SoLuong1 == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    temp_double = double.Parse(DVD2) * double.Parse(DVD2) * double.Parse(DVD1) / (4 * 3.1415926 * 1000000000);
                    SoKhoi_d = temp_double;
                    count = 1;
                }

            }
            if (SoKhoi_d > double.Parse(lb_TonKho_outputKG.Text))
            {
                MessageBox.Show("Số khối xuất hiện đã vượt quá số khối tồn kho");
                txt_SLGT_outputKG.Text = "";
                txt_SLPhach_outputKG.Text = "";
                txt_SLVan_outputKG.Text = "";
            }
        }
        private void cb_QuyCach_outputKG_Leave(object sender, EventArgs e)
        {

            if (rd_Phach_outputKG.IsChecked == true)
            {
                if (cb_QuyCach_outputKG.Text != "")
                {
                    for (i = 0; i < cb_QuyCach_outputKG.Items.Count; i++)
                    {
                        if (cb_QuyCach_outputKG.Text == cb_QuyCach_outputKG.Items[i].ToString())
                        {
                            count++;
                            break;
                        }
                    }
                    if (count == 0)
                        MessageBox.Show("Nhập sai quy cách");
                    else
                    {


                        Conn = prs.Connect();
                        Conn.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = Conn;
                        try
                        {
                            string_temp = prs.substring_right(cb_QuyCach_outputKG.Text, child);
                            string[] temp = string_temp.Split('x');
                            DVD1 = temp[0].ToString().Trim();
                            DVD2 = temp[1].ToString().Trim();
                            DVD3 = temp[2].ToString().Trim();
                            temp.Clone();
                            cmd.CommandText = "select KhoGo_TK.SoKhoiTon from dbo.KhoGo_TK where DVD1=" + DVD1 + " and DVD2=" + DVD2 + " and DVD3=" + DVD3 + " and ADorKD='" + cb_AdorKD_outputKG.Text + "'";
                            //MessageBox.Show(cmd.CommandText.ToString());
                            lb_TonKho_outputKG.Text = cmd.ExecuteScalar().ToString();
                            

                        }
                        catch (Exception ex)
                        {
                            DialogResult Err;
                            Err = MessageBox.Show("ĐÃ XẢY RA LỖI. DỮ LIỆU KHÔNG ĐÚNG, HOẶC QUY CÁCH CHƯA PHÁT SINH " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            lb_TonKho_outputKG.Text = "0";
                        }
                        finally
                        {
                            Conn.Close();
                            cmd.Dispose();
                        }
                    }
                }

            }
        }

        #endregion

       
        #region //Chung Tu
        private void search_CTKG()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = "prc_Search_KGinput";
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {

                Serial1 = txt_SerialCT_CTKG.Text;
                cmd.Parameters.AddWithValue("@Serial", Serial1);
                if (radGridView3.Rows.Count > 0)
                    radGridView3.Rows.Clear();
                DataSet ds = new DataSet();
                ds = prs.dts_sql(cmd, Conn, ds);
                //prs.reset_page_txt(Page_ChungTu);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    cb_NoiGiao_CTKG.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                    cb_NoiNhan_CTKG.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                    cb_AdorKD_CTKG.Text = ds.Tables[0].Rows[0]["ADorKD"].ToString();
                    cb_NguonGo_CTKG.Text = ds.Tables[0].Rows[0]["LoaiGo"].ToString();
                    cb_LyDo_CTKG.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                    txt_BienBan_CTKG.Text = ds.Tables[0].Rows[0]["BienBan"].ToString();
                    txt_ChungTu_CTKG.Text = ds.Tables[0].Rows[0]["ChungTu"].ToString();
                    txt_SoXe_CTKG.Text = ds.Tables[0].Rows[0]["SoXe"].ToString();
                    dtp_Date_CTKG.Text = ds.Tables[0].Rows[0]["Ngay"].ToString();

                    temp_double = 0;
                    SoKhoi_d = 0;

                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        radGridView3.Rows.Add(ds.Tables[0].Rows[i]["DVD1"].ToString(), ds.Tables[0].Rows[i]["DVD2"].ToString(), ds.Tables[0].Rows[i]["DVD3"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString(), ds.Tables[0].Rows[i]["SLXuat"].ToString(), ds.Tables[0].Rows[i]["SoKhoiXuat"].ToString());
                        temp_double += double.Parse(ds.Tables[0].Rows[i]["SLXuat"].ToString()) + double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                        SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiXuat"].ToString());
                    }
                    lb_SoKhoi_CTKG.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                    lb_SoLuong_CTKG.Text = string.Format("{0:###,###}", temp_double);
                    string_temp = ds.Tables[0].Rows[0]["Nhom"].ToString();
                    if (string_temp == "Phach")
                    {
                        radGridView3.Columns["DVD1_CTKG"].HeaderText = "C Dày";
                        radGridView3.Columns["DVD2_CTKG"].HeaderText = "C Rộng";
                        radGridView3.Columns["DVD3_CTKG"].HeaderText = "C Dài";
                    }
                    if (string_temp == "Van")
                    {
                        radGridView3.Columns["DVD1_CTKG"].HeaderText = "C Dày";
                        radGridView3.Columns["DVD2_CTKG"].HeaderText = "";
                        radGridView3.Columns["DVD3_CTKG"].HeaderText = "";
                    }
                    if (string_temp == "GoTron")
                    {
                        radGridView3.Columns["DVD2_CTKG"].HeaderText = "C Dài";
                        radGridView3.Columns["DVD1_CTKG"].HeaderText = "Vanh";
                        radGridView3.Columns["DVD3_CTKG"].HeaderText = "";
                    }
                }
                else
                    MessageBox.Show("Không có dữ liệu vui lòng kiểm tra lại thông tin tìm kiếm");
                ds.Clear();
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("Kiểm tra lại Serial " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_Search_CTKG_Click(object sender, EventArgs e)
        {
            search_CTKG();
        }
        private void edit_searchCT()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;

            Serial1 = txt_SerialCT_CTKG.Text;
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SERIAL "+Serial1+" KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (TL == DialogResult.Yes)
            {
                try
                {
                    if (cb_NoiNhan_CTKG.Text != "" && cb_NoiNhan_CTKG.Text != "" && cb_LyDo_CTKG.Text != "" && cb_AdorKD_CTKG.Text != "" && cb_NguonGo_CTKG.Text != ""||txt_SerialCT_CTKG.Text!="")
                    {
                        NoiGiao1 = prs.substring_left(cb_NoiGiao_CTKG.Text, child);
                        if (NoiGiao1 == "")
                            NoiGiao1 = cb_NoiGiao_CTKG.Text;
                        NoiNhan1 = prs.substring_left(cb_NoiNhan_CTKG.Text, child);
                        if (NoiNhan1 == "")
                            NoiNhan1 = cb_NoiNhan_CTKG.Text;
                        LyDo1 = prs.substring_right(cb_LyDo_CTKG.Text, child);
                        if (LyDo1 == "")
                            LyDo1 = cb_LyDo_CTKG.Text;
                        count = 0;
                        for (i = 0; i < cb_NoiNhan_CTKG.Items.Count; i++)
                        {
                            if (NoiGiao1 == prs.substring_left(cb_NoiNhan_CTKG.Items[i].ToString(),child))
                                count++;
                            if (NoiNhan1 == prs.substring_left(cb_NoiNhan_CTKG.Items[i].ToString(),child))
                                count++;
                            if (count == 2)
                                break;

                        }
                        if (count == 2)
                        {

                            for (i = 0; i < cb_LyDo_CTKG.Items.Count; i++)
                            {
                                if (LyDo1 == prs.substring_right(cb_LyDo_CTKG.Items[i].ToString(), child))
                                {
                                    count++;
                                    break;
                                }
                            }
                            if (count == 3)
                            {

                                
                                ADorKD1 = cb_AdorKD_CTKG.Text.Trim();
                                LoaiGo1 = cb_NguonGo_CTKG.Text.Trim();
                                
                                BienBan1 = txt_BienBan_CTKG.Text.Trim();
                                ChungTu1 = txt_ChungTu_CTKG.Text.Trim();
                                SoXe1 = txt_SoXe_CTKG.Text.Trim();
                                Ngay1 = Date_Split(dtp_Date_CTKG.Text);
                                cmd.CommandText = @"UPDATE [ChungTuNhap] SET [NoiGiao] ='" + NoiGiao1 + "' ,[NoiNhan] ='" + NoiNhan1 + "' ,[ADorKD] = '" + ADorKD1 + "',[LoaiGo] ='" + LoaiGo1 + "' ,[LyDo] ='" + LyDo1 + "' ,[BienBan] ='" + BienBan1 + "' ,[ChungTu] ='" + ChungTu1 + "' ,[SoXe] ='" + SoXe1 + "' ,[Ngay] ='" + Ngay1 + "' WHERE Serial= " + Serial1;
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Dữ liệu được sửa thành công");
                                radGridView3.Rows.Clear();
                                txt_SerialCT_CTKG.Text = "";
                            }
                            else
                                MessageBox.Show("Kiểm tra lại Lý do");
                        }
                        else
                            MessageBox.Show("Kiểm tra lại Nơi giao hoặc Nơi nhận");
                    }
                    else
                        MessageBox.Show("Nhập thiếu thông tin");

                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void bt_edit_CTKG_Click(object sender, EventArgs e)
        {
            edit_searchCT();
        }

        private void bt_delete_CTKG_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;

            Serial1 = txt_SerialCT_CTKG.Text;
            DialogResult TL;
            MessageBox.Show("TẤT CẢ CÁC LÔ HÀNG TRONG DANH SÁCH PHÍA DƯỚI SẼ BỊ XÓA??");
            TL = MessageBox.Show("BẠN CÓ MUỐN XÓA SERIAL " + Serial1 + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (TL == DialogResult.Yes)
            {
                try
                {
                    cmd.CommandText = @"DELETE FROM [ChungTuNhap] WHERE Serial=" + Serial1;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được xóa thành công");
                    radGridView3.Rows.Clear();

                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("ĐÃ XẢY RA LỖI, XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        #endregion
        #region // Chuyen
        private void addgrid_Transfer()
        {
            try
            {
               
                count = 0;
                SoKhoi_d = 0;
                /*NoiGiao1 = prs.substring_left(cb_NoiGiao_TranKG.Text, child);
                NoiNhan1 = prs.substring_left(cb_NoiNhan_TranKG.Text, child);
                ADorKD1 = cb_AdorKD_TranKG.Text.Trim();
                PhanLoai1 = cb_PhanLoai_TranKG.Text.Trim();
                LyDo1 = prs.substring_right(cb_LoaiGo_TranKG.Text, child);
                BienBan1 = txt_BienBan_TranKG.Text.Trim();
                ChungTu1 = txt_BienBan_TranKG.Text.Trim();
                Ngay1 = Date_Split(dtp_Date_TranKG.Text);
                SoXe1 = txt_SoXe_TranKG.Text.Trim();
                ChungTu1 = txt_ChungTu_TranKG.Text.Trim();
                */
                if (rd_Phach_TranKG.IsChecked == true)
                {
                    for (i = 0; i < cb_QuyCach_TranKG.Items.Count; i++)
                    {
                        if (cb_QuyCach_TranKG.Text == cb_QuyCach_TranKG.Items[i].ToString())
                        {
                            count++;
                           
                        }
                        if (cb_QuyCach_TranKG1.Text == cb_QuyCach_TranKG.Items[i].ToString())
                        {
                            count++;
                        }
                        if (count == 2)
                            break;
                    }
                    if (count <2)
                        MessageBox.Show("Nhập sai quy cách");
                    else
                    {

                        string_temp = prs.substring_right(cb_QuyCach_TranKG.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                        PhanLoai1 = cb_PhanLoai_TranKG.Text.Trim();
                        SoLuong1 = txt_SLPhach_TranKG.Text.Trim();
                        temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                        SoKhoi1 = temp_double.ToString();
                        string_temp = prs.substring_right(cb_QuyCach_TranKG1.Text, child);
                        string[] temp1 = string_temp.Split('x');
                        DVD1T = temp1[0].ToString().Trim();
                        DVD2T = temp1[1].ToString().Trim();
                        DVD3T = temp1[2].ToString().Trim();
                        temp1.Clone();
                        temp_double = double.Parse(DVD1T) * double.Parse(DVD2T) * double.Parse(DVD3T) * double.Parse(SoLuong1) / 1000000000;
                        SoKhoi1T = temp_double.ToString();
                        count = 3;

                    }


                }
                if (rd_Van_TranKG.IsChecked == true)
                {

                    DVD1 = txt_CDay_TranKG.Text.Trim();
                    DVD1T = txt_CDay_TranKG1.Text;
                    SoLuong1 = txt_SLVan_TranKG.Text.Trim();
                    SoKhoi1 = txt_SoKhoiVan_TranKG.Text.Trim();
                    if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        count = 3;
                        DVD2 = "";
                        DVD2T = "";
                        DVD3 = "";
                        DVD3T = "";
                        SoKhoi1T = SoKhoi1;
                    }

                }
                if (rd_GoTron_TranKG.IsChecked == true)
                {
                    DVD2 = txt_CDaiGT_TranKG.Text.Trim();
                    DVD1 = txt_VanhGT_TranKG.Text.Trim();
                    SoLuong1 = txt_SLGT_TranKG.Text.Trim();
                    DVD2T = txt_CDaiGT_TranKG1.Text;
                    DVD1T = txt_VanhGT_TranKG1.Text;
                    if (DVD1 == "" || DVD2 == "" || SoLuong1 == ""||DVD1T==""||DVD2T=="")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        temp_double = double.Parse(DVD2) * double.Parse(DVD1) * double.Parse(DVD1)*double.Parse(SoLuong1) / (4 * 3.1415926 * 1000000000);
                         if(double.Parse(lb_TonKho_TranKG.Text)<temp_double)
                            count = 3;

                        SoKhoi1 = temp_double.ToString();
                        temp_double = double.Parse(DVD2T) * double.Parse(DVD1T) * double.Parse(DVD1T) * double.Parse(SoLuong1) / (4 * 3.1415926 * 1000000000);
                        SoKhoi1T = temp_double.ToString();
                       
                        DVD3 = "";
                        DVD3T = "";

                    }
                }
                if (count == 3)
                {
                    radGridView4.Rows.Add(DVD1, DVD2, DVD3, "-"+SoLuong1, "-"+SoKhoi1, PhanLoai1);
                    radGridView4.Rows.Add(DVD1T, DVD2T, DVD3T, SoLuong1, SoKhoi1T, PhanLoai1);
                    //temp_double=double.Parse(
                    cb_QuyCach_TranKG.Text = "";
                    cb_PhanLoai_TranKG.Text = "";
                    txt_SLPhach_TranKG.Text = "";
                    txt_CDay_TranKG.Text = "";
                    txt_SLVan_TranKG.Text = "";
                    txt_SoKhoiVan_TranKG.Text = "";
                    txt_CDaiGT_TranKG.Text = "";
                    txt_VanhGT_TranKG.Text = "";
                    txt_SLGT_TranKG.Text = "";
                    if (radGridView4.Rows.Count == 2)
                    {

                        if (rd_Phach_TranKG.IsChecked == true)
                        {
                            rd_GoTron_TranKG.Enabled = false;
                            rd_Van_TranKG.Enabled = false;
                            txt_CDay_TranKG.Enabled = false;
                            txt_SLVan_TranKG.Enabled = false;
                            txt_SoKhoiVan_TranKG.Enabled = false;
                            txt_CDaiGT_TranKG.Enabled = false;
                            txt_VanhGT_TranKG.Enabled = false;
                            txt_SLGT_TranKG.Enabled = false;

                        }
                        if (rd_Van_TranKG.IsChecked == true)
                        {
                            rd_GoTron_TranKG.Enabled = false;
                            rd_Phach_TranKG.Enabled = false;
                            txt_CDaiGT_TranKG.Enabled = false;
                            txt_VanhGT_TranKG.Enabled = false;
                            txt_SLGT_TranKG.Enabled = false;
                            cb_QuyCach_TranKG.Enabled = false;
                            txt_SLPhach_TranKG.Enabled = false;
                            cb_PhanLoai_TranKG.Enabled = false;
                        }
                        if (rd_GoTron_TranKG.IsChecked == true)
                        {
                            rd_Van_TranKG.Enabled = false;
                            rd_Phach_TranKG.Enabled = false;
                            cb_QuyCach_TranKG.Enabled = false;
                            txt_SLPhach_TranKG.Enabled = false;
                            cb_PhanLoai_TranKG.Enabled = false;
                            txt_CDay_TranKG.Enabled = false;
                            txt_SLVan_TranKG.Enabled = false;
                            txt_SoKhoiVan_TranKG.Enabled = false;
                        }
                    }
                }
                    
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
        private void Checked_SoChePhach_Tran()
        {
            cb_QuyCach_TranKG.Enabled = true;
            cb_PhanLoai_TranKG.Enabled = true;
            txt_SLPhach_TranKG.Enabled = true;
            cb_QuyCach_TranKG1.Enabled = true;
            txt_CDay_TranKG.Enabled = false;
            txt_SLVan_TranKG.Enabled = false;
            txt_SoKhoiVan_TranKG.Enabled = false;

            txt_CDaiGT_TranKG.Enabled = false;
            txt_VanhGT_TranKG.Enabled = false;
            txt_SLGT_TranKG.Enabled = false;
            txt_CDay_TranKG1.Enabled = false;
            txt_CDaiGT_TranKG1.Enabled = false;
            txt_VanhGT_TranKG1.Enabled = false;
            radGridView4.Columns["DVD1_TranKG"].HeaderText = "C Dày";
            radGridView4.Columns["DVD2_TranKG"].HeaderText = "C Rộng";
            radGridView4.Columns["DVD3_TranKG"].HeaderText = "C Dài";
            


        }
        private void Checked_Van_Tran()
        {
            cb_QuyCach_TranKG.Enabled = false;
            cb_QuyCach_TranKG1.Enabled = false;
            cb_PhanLoai_TranKG.Enabled = false;
            txt_SLPhach_TranKG.Enabled = false;
            txt_CDay_TranKG.Enabled = true;
            txt_SLVan_TranKG.Enabled = true;
            txt_SoKhoiVan_TranKG.Enabled = true;
            txt_CDay_TranKG1.Enabled = true;
            txt_CDaiGT_TranKG.Enabled = false;
            txt_CDaiGT_TranKG1.Enabled = false;
            txt_VanhGT_TranKG.Enabled = false;
            txt_SLGT_TranKG.Enabled = false;
            txt_VanhGT_TranKG1.Enabled = false;
            radGridView4.Columns["DVD1_TranKG"].HeaderText = "C Dày";
            radGridView4.Columns["DVD2_TranKG"].HeaderText = " ";
            radGridView4.Columns["DVD3_TranKG"].HeaderText = " ";

        }
        private void Checked_GoTron_Tran()
        {
            cb_QuyCach_TranKG.Enabled = false;
            cb_QuyCach_TranKG1.Enabled = false;
            cb_PhanLoai_TranKG.Enabled = false;
            txt_SLPhach_TranKG.Enabled = false;
            txt_CDay_TranKG.Enabled = false;
            txt_SLVan_TranKG.Enabled = false;
            txt_SoKhoiVan_TranKG.Enabled = false;
            txt_CDay_TranKG1.Enabled = false;
            txt_CDaiGT_TranKG.Enabled = true;
            txt_VanhGT_TranKG.Enabled = true;
            txt_SLGT_TranKG.Enabled = true;
            txt_CDaiGT_TranKG1.Enabled = true;
            txt_VanhGT_TranKG1.Enabled = true;
            radGridView4.Columns["DVD2_TranKG"].HeaderText = "C Dài";
            radGridView4.Columns["DVD1_TranKG"].HeaderText = "Vanh";
            radGridView4.Columns["DVD3_TranKG"].HeaderText = " ";

        }
        private void cb_AdorKD_TranKG_Leave(object sender, EventArgs e)
        {
            if (cb_AdorKD_TranKG.Text != "AD" && cb_AdorKD_TranKG.Text != "KD")
            {
                MessageBox.Show("Nhập sai thông tin");
                cb_AdorKD_TranKG.Focus();
            }
        }

        private void rd_insert_TranKG_Click(object sender, EventArgs e)
        {
            addgrid_Transfer();
        }

        private void bt_rework_TranKG_Click(object sender, EventArgs e)
        {
            if (radGridView4.Rows.Count > 0)
                radGridView4.Rows.Clear();
            rd_Phach_TranKG.Enabled = true;
            rd_Van_TranKG.Enabled = true;
            rd_GoTron_TranKG.Enabled = true;
            prs.reset_page_txt(Page_Chuyen);
        }

        private void rd_Phach_TranKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_SoChePhach_Tran();
        }

        private void rd_Van_TranKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_Van_Tran();
        }

        private void rd_GoTron_TranKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_GoTron_Tran();
        }

        private void cb_QuyCach_TranKG_Leave(object sender, EventArgs e)
        {
            if (rd_Phach_TranKG.IsChecked == true)
            {
                if (cb_QuyCach_TranKG.Text != "")
                {
                    for (i = 0; i < cb_QuyCach_TranKG.Items.Count; i++)
                    {
                        if (cb_QuyCach_TranKG.Text == cb_QuyCach_TranKG.Items[i].ToString())
                        {
                            count++;
                            break;
                        }
                    }
                    if (count == 0)
                        MessageBox.Show("Nhập sai quy cách");
                    else
                    {


                        Conn = prs.Connect();
                        Conn.Open();
                        SqlCommand cmd = new SqlCommand();
                        cmd.Connection = Conn;
                        try
                        {
                            string_temp = prs.substring_right(cb_QuyCach_TranKG.Text, child);
                            string[] temp = string_temp.Split('x');
                            DVD1 = temp[0].ToString().Trim();
                            DVD2 = temp[1].ToString().Trim();
                            DVD3 = temp[2].ToString().Trim();
                            temp.Clone();
                            cmd.CommandText = "select KhoGo_TK.SoKhoiTon from dbo.KhoGo_TK where DVD1=" + DVD1 + " and DVD2=" + DVD2 + " and DVD3=" + DVD3 + " and ADorKD='" + cb_AdorKD_TranKG.Text + "'";
                            //MessageBox.Show(cmd.CommandText.ToString());
                            lb_TonKho_TranKG.Text = cmd.ExecuteScalar().ToString();
                        }
                        catch (Exception ex)
                        {
                            DialogResult Err;
                            Err = MessageBox.Show("ĐÃ XẢY RA LỖI. DỮ LIỆU KHÔNG ĐÚNG, HOẶC QUY CÁCH CHƯA PHÁT SINH " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            lb_TonKho_TranKG.Text = "0";
                        }
                        finally
                        {
                            Conn.Close();
                            cmd.Dispose();
                        }
                    }
                }
            }

        }

        private void txt_CDay_TranKG_Leave(object sender, EventArgs e)
        {
            if (txt_CDay_TranKG.Text != "")
            {
                prs.checktext_number(txt_CDay_TranKG);
                Conn = prs.Connect();
                SqlCommand cmd = new SqlCommand();

                try
                {
                    Conn.Open();
                    cmd.Connection = Conn;
                    cmd.CommandText = @"select SoKhoiTon from dbo.KhoGo_TK where DVD1=" + txt_CDay_TranKG.Text + " and DVD2=0 and ADorKD='" + cb_AdorKD_TranKG.Text + "'";
                    lb_TonKho_TranKG.Text = cmd.ExecuteScalar().ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    lb_TonKho_TranKG.Text = "0";
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }


        }

        private void txt_VanhGT_TranKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_VanhGT_TranKG);
            
        }
        private void txt_CDaiGT_TranKG_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_CDaiGT_TranKG);
            if (txt_CDaiGT_TranKG.Text != "")
            {

                if (txt_VanhGT_TranKG.Text == "")
                {
                    MessageBox.Show("Bạn cần nhập vanh");
                }
                else
                {

                    txt_SLGT_TranKG.Focus();
                    Conn = prs.Connect();
                    SqlCommand cmd = new SqlCommand();

                    try
                    {
                        Conn.Open();
                        cmd.Connection = Conn;
                        cmd.CommandText = @"select sum(SoKhoiTon) from dbo.KhoGo_TK where Nhom='GoTron' and ADorKD='"+cb_AdorKD_TranKG.Text+"'";
                        //MessageBox.Show(cmd.CommandText.ToString());
                        lb_TonKho_TranKG.Text = cmd.ExecuteScalar().ToString();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        lb_TonKho_TranKG.Text = "0";
                    }
                    finally
                    {
                        Conn.Close();
                        cmd.Dispose();
                    }
                }
            }

        }
        private void save_Tran()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Tran_Ins_CTNhap";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;

            try
            {

                //string insert_NhapKTam = null;
                if (cb_AdorKD_TranKG.Text == "" || cb_NguonGo_TranKG.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {


                    ADorKD1 = cb_AdorKD_TranKG.Text.Trim();
                    LoaiGo1 = cb_NguonGo_TranKG.Text.Trim();
                    ChungTu1 = txt_ChungTu_TranKG.Text.Trim();

                    Ngay1 = Date_Split(dtp_Date_TranKG.Text);

                    cmd.Parameters.AddWithValue("@NoiGiao", "Trans");
                    cmd.Parameters.AddWithValue("@NoiNhan", "Trans");
                    cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                    cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                    cmd.Parameters.AddWithValue("@LyDo", "Chuyen");
                    cmd.Parameters.AddWithValue("@BienBan", "");
                    cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                    cmd.Parameters.AddWithValue("@SoXe", "");
                    cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                    cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                    SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                    code.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(code);
                    SqlDataReader sdr = cmd.ExecuteReader();
                    Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                    sdr.Close();
                    cmd.Parameters.Clear();

                    if (rd_Phach_TranKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapPhach";
                        for (i = radGridView4.Rows.Count - 1; i >= 0; i--)
                        {
                            DVD1 = radGridView4.Rows[i].Cells["DVD1_TranKG"].Value.ToString();
                            DVD2 = radGridView4.Rows[i].Cells["DVD2_TranKG"].Value.ToString();
                            DVD3 = radGridView4.Rows[i].Cells["DVD3_TranKG"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView4.Rows[i].Cells["SoLuong_TranKG"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView4.Rows[i].Cells["SoKhoi_TranKG"].Value.ToString();
                            PhanLoai1 = radGridView4.Rows[i].Cells["PhanLoai_TranKG"].Value.ToString();
                            DVT1 = "";
                            KhachHang1 = "";
                            GhiChu1 = "";
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            //cmd.Parameters.AddWithValue("@flag", "1");
                            cmd.Parameters.AddWithValue("@Wait", "2");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView4.Rows.RemoveAt(i);
                        }
                    }

                    if (rd_Van_TranKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapVan";
                        for (i = radGridView4.Rows.Count - 1; i >= 0; i--)
                        {
                            DVD1 = radGridView4.Rows[i].Cells["DVD1_TranKG"].Value.ToString();
                            // DVD2 = radGridView4.Rows[i].Cells["DVT2_TranKG"].Value.ToString();
                            //DVD3 = radGridView4.Rows[i].Cells["DVT3_TranKG"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView4.Rows[i].Cells["SoLuong_TranKG"].Value.ToString()).ToString();
                            SoKhoi1 = radGridView4.Rows[i].Cells["SoKhoi_TranKG"].Value.ToString();
                            //PhanLoai1 = radGridView4.Rows[i].Cells["PhanLoai_TranKG"].Value.ToString();
                            DVT1 = "";
                            KhachHang1 = "";
                            GhiChu1 = "";
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", SoKhoi1);
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            //cmd.Parameters.AddWithValue("@flag", "1");
                            cmd.Parameters.AddWithValue("@Wait", "2");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView4.Rows.RemoveAt(i);
                        }


                    }
                    if (rd_GoTron_TranKG.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapGT";
                        for (i = radGridView4.Rows.Count - 1; i >= 0; i--)
                        {
                            DVD1 = double.Parse(radGridView4.Rows[i].Cells["DVD1_TranKG"].Value.ToString()).ToString();
                            DVD2 = double.Parse(radGridView4.Rows[i].Cells["DVD2_TranKG"].Value.ToString()).ToString();
                            //DVD3 = radGridView4.Rows[i].Cells["DVT3_TranKG"].Value.ToString();
                            SoLuong1 = SoLuong1 = double.Parse(radGridView4.Rows[i].Cells["SoLuong_TranKG"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView4.Rows[i].Cells["SoKhoi_TranKG"].Value.ToString();
                            //PhanLoai1 = radGridView4.Rows[i].Cells["PhanLoai_TranKG"].Value.ToString();
                            DVT1 = "";
                            KhachHang1 = "";
                            GhiChu1 = "";


                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            //cmd.Parameters.AddWithValue("@flag", "1");
                            cmd.Parameters.AddWithValue("@Wait", "2");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView4.Rows.RemoveAt(i);
                        }
                    }
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    bt_rework_TranKG_Click(null, null);
                }
            }

            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
 

        }
        private void bt_save_TranKG_Click(object sender, EventArgs e)
        {
            save_Tran();
        }

        private void txt_SLPhach_TranKG_Leave(object sender, EventArgs e)
        {
            if (txt_SLPhach_TranKG.Text != "")
            {
                try
                {
                    prs.checktext_number(txt_SLPhach_TranKG);
                    if (chk_ChoXuLy.Checked == false || chk_ChoXuLy.Enabled == false)
                    {
                        string_temp = prs.substring_right(cb_QuyCach_TranKG.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                        SoKhoi_d = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(txt_SLPhach_TranKG.Text) / 1000000000;
                        if (SoKhoi_d > double.Parse(lb_TonKho_TranKG.Text))
                        {
                            //MessageBox.Show(SoKhoi_d.ToString());
                            MessageBox.Show("Số khối đã vượt quá số khối tồn kho, số lượng cho phép <" + string.Format("{0:###,###,###}", (double.Parse(lb_TonKho_TranKG.Text) * 1000000000 / (double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3)))));
                            txt_SLPhach_TranKG.Text = "";
                            txt_SLPhach_TranKG.Focus();
                        }
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

        }

        private void txt_SoKhoiVan_TranKG_Leave(object sender, EventArgs e)
        {
            if (txt_SoKhoiVan_TranKG.Text != "")
            {
                prs.checktext_number(txt_SoKhoiVan_TranKG);
                if (chk_ChoXuLy.Checked == false || chk_ChoXuLy.Enabled == false)
                {
                    if (double.Parse(txt_SoKhoiVan_TranKG.Text) > double.Parse(lb_TonKho_TranKG.Text))
                    {
                        MessageBox.Show("Số khối nhập đã vượt quá số khối tồn kho");
                        txt_SoKhoiVan_TranKG.Text = "";
                        txt_SoKhoiVan_TranKG.Focus();
                    }
                }
            }

        }

        private void txt_SLGT_TranKG_Leave(object sender, EventArgs e)
        {
            if (txt_SLGT_TranKG.Text != "")
            {
                prs.checktext_number(txt_SLGT_TranKG);
                if (chk_ChoXuLy.Enabled == false || chk_ChoXuLy.Checked == false)
                {
                    try
                    {
                        if (txt_CDaiGT_TranKG.Text != "" && txt_VanhGT_TranKG.Text != "")
                        {
                            temp_double = double.Parse(txt_VanhGT_TranKG.Text);
                            SoLuong_d = double.Parse(txt_SLGT_TranKG.Text);
                            SoKhoi_d = temp_double * temp_double * SoLuong_d*double.Parse(txt_CDaiGT_TranKG.Text) / (4 * 3.1415926 * 1000000000);
                            if (SoKhoi_d > double.Parse(lb_TonKho_TranKG.Text))
                            {
                                MessageBox.Show("Số khối xuất vượt quá số khối tồn kho");
                                txt_SLGT_TranKG.Text = "";
                                txt_SLGT_TranKG.Focus();
                            }
                        }


                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }

            }

        }
        #endregion
        #region //TonKho
        private void TonKho()
        {
            Conn = prs.Connect();
            Conn.Open();
            Datebegin = Date_Split(dtp_Datebegin_TKKG.Text);
            Dateend = Date_Split(dtp_Dateend_TKKG.Text);
            DataSet ds = new DataSet();
            Datebegin = Datebegin + " 00:00";
            Dateend = Dateend + " 23:59";
            
            string_temp = @"select qry1.DVD1,qry1.DVD2,qry1.DVD3,sum(qry1.SLTonDau) as SLTonDau,sum(qry1.SKTonDau) as SKTonDau,sum(qry1.SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(qry1.SoKhoiNhap) as SoKhoiNhap,SUM(qry1.SoKhoiXuat) as SoKhoiXuat,sum(SLTonDau)+sum(SLNhap)-sum(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon,qry1.Nhom,qry1.ADorKD
                        from
                        (select dbo.NhapGo.DVD1,dbo.NhapGo.DVD2,dbo.NhapGo.DVD3,SLTonDau=0,SKTonDau=0,sum(SLNhap) as SLNhap,sum(SLXuat) as SLXuat,sum(NhapGo.SoKhoiNhap) as SoKhoiNhap,sum(dbo.NhapGo.SoKhoiXuat) as SoKhoiXuat,Nhom,ADorKD
                        from dbo.NhapGo where Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "' group by DVD1,DVD2,DVD3,Nhom,ADorKD";
            string_temp += @"                union all
                        select dbo.NhapGo.DVD1,dbo.NhapGo.DVD2,dbo.NhapGo.DVD3,sum(SLNhap)-sum(SLXuat) as SLTonDau,sum(NhapGo.SoKhoiNhap)-sum(dbo.NhapGo.SoKhoiXuat) as TonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0,Nhom,ADorKD
                        from dbo.NhapGo where Ngay<'" + Datebegin + "'group by DVD1,DVD2,DVD3,Nhom,ADorKD) as qry1 ";
            if (rd_Phach_TKKG.IsChecked == true)
            {
                string_temp += " where Nhom='Phach'";
            }
            if (rd_Van_TKKG.IsChecked == true)
            {
                string_temp += " where Nhom='Van'";
            }
            if (rd_GoTron_TKKG.IsChecked == true)
            {
                string_temp += " where Nhom='GoTron'";
            }
            if (cb_AdorKD_TKKG.Text == "")
                MessageBox.Show("Bạn phải nhập thông tin AD/KD");
            else
            {
                string_temp += " and ADorKD='" + cb_AdorKD_TKKG.Text + "'";
                try
                {

                    if (txt_DVD1_TKKG.Text != "")
                    {
                        string_temp += " and DVD1=" + txt_DVD1_TKKG.Text;
                    }
                    if (txt_DVD2_TKKG.Text != "")
                        string_temp += " and DVD2=" + txt_DVD2_TKKG.Text;
                    if (txt_DVD3_TKKG.Text != "")
                        string_temp += " and DVD3=" + txt_DVD3_TKKG.Text;
                    string_temp += " group by qry1.DVD1,qry1.DVD2,qry1.DVD3,Nhom,ADorKD";
                    ds = prs.dts(string_temp, Conn, ds);
                    if (radGridView5.Rows.Count > 0)
                    {
                        for (i = 0; i < this.radGridView5.ChildRows.Count; i++)
                        {
                            GridViewGroupRowInfo groupRowInfo = this.radGridView5.ChildRows[i] as GridViewGroupRowInfo;
                            groupRowInfo.HeaderText = "";
                        }
                    }
                    radGridView5.DataSource = ds.Tables[0];
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        SLNhap_d = 0;
                        SLXuat_d = 0;
                        //count = 0;
                        SoKhoi_d = 0;
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            SLNhap_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString());
                            SLXuat_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiXuat"].ToString());
                            //SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoi"].ToString());
                            SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiTon"].ToString());
                        }
                        lb_SLNhap_KV5Search.Text = string.Format("{0:###,###,###.#}", SLNhap_d);
                        lb_SLXuat_KV5Search.Text = string.Format("{0:###,###,###.#}", SLXuat_d);
                        lb_SLTon_KV5Search.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                        //lb_SLPallet_KV5Search.Text = string.Format("{0:###,###}", i);
                        for (i = 0; i < this.radGridView5.ChildRows.Count; i++)
                        {
                            //this.radGridView5.ChildRows[i].Cells["Serial_KV5Search"].Value = i.ToString();

                            GridViewGroupRowInfo groupRowInfo = this.radGridView5.ChildRows[i] as GridViewGroupRowInfo;
                            if (groupRowInfo != null)
                            {
                                SLNhap_d = 0;
                                SLXuat_d = 0;
                                //groupRowInfo.HeaderText = groupRowInfo.HeaderText + " " + i.ToString();
                                for (int p = 0; p < this.radGridView5.ChildRows[i].ChildRows.Count; p++)
                                {
                                    //this.radGridView5.ChildRows[i].ChildRows[p].Cells["Serial_KV5Search"].Value = p.ToString();
                                    SLNhap_d += double.Parse(radGridView5.ChildRows[i].ChildRows[p].Cells["SLTon_TKKG"].Value.ToString());
                                    SLXuat_d += double.Parse(radGridView5.ChildRows[i].ChildRows[p].Cells["SKTon_TKKG"].Value.ToString());
                                }
                                string_temp = groupRowInfo.HeaderText + "            SL Tồn: " + SLNhap_d.ToString() + "            SK Tồn: " + SLXuat_d.ToString();
                                groupRowInfo.HeaderText = string_temp;

                            }
                        }
                    }
                    else
                        MessageBox.Show("Không có dữ liệu");



                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();

                }
            }
        }

        private void rd_Phach_TKKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView5.Columns["DVD1_TKKG"].HeaderText = "C Dày";
            radGridView5.Columns["DVD2_TKKG"].HeaderText = "C Rộng";
            radGridView5.Columns["DVD3_TKKG"].HeaderText = "C Dài";
        }

        private void rd_Van_TKKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView5.Columns["DVD1_TKKG"].HeaderText = "C Dày";
            radGridView5.Columns["DVD2_TKKG"].HeaderText = "";
            radGridView5.Columns["DVD3_TKKG"].HeaderText = "";
        }

        private void rd_GoTron_TKKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView5.Columns["DVD1_TKKG"].HeaderText = "Vanh";
            radGridView5.Columns["DVD2_TKKG"].HeaderText = "C Dài";
            radGridView5.Columns["DVD3_TKKG"].HeaderText = "";
        }

        private void radButton1_Click(object sender, EventArgs e)
        {
            TonKho();
        }

        private void bt_excelexport_TKKG_Click(object sender, EventArgs e)
        {

            if (radGridView5.Rows.Count > 1)
            {
                DataTable dt = (DataTable)radGridView5.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\TonKhoKG.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }
            
                            //
        
        #endregion

        #region//Bao Cao
        private DataSet Report_KG()
        {
            
                DataSet ds = new DataSet();
                Conn = prs.Connect();
                Conn.Open();
                Datebegin = Date_Split(dtp_Datebegin_BCKG.Text);
                Datebegin = Datebegin + " 00:00";
                Dateend = Date_Split(dtp_Dateend_BCKG.Text);
                Dateend = Dateend + " 23:59";
                GhiChu1 = "";
                DVT1 = "";
                if (cb_NoiGiao_BCKG.Text != "")
                {
                    GhiChu1 += " and NoiGiao='" + prs.substring_left(cb_NoiGiao_BCKG.Text, child) + "'";
                    DVT1 += " Nơi giao: " + prs.substring_right(cb_NoiGiao_BCKG.Text, child) + "\n";
                }
                if (cb_NoiNhan_BCKG.Text != "")
                {
                    GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_BCKG.Text, child) + "'";
                    DVT1 += " Nơi nhận: " + prs.substring_right(cb_NoiNhan_BCKG.Text, child) + "\n";
                }
                if (cb_AdorKD_BCKG.Text != "")
                {
                    GhiChu1 += " and ADorKD='" + cb_AdorKD_BCKG.Text + "'";
                    DVT1 += " ADorKD: " + cb_AdorKD_BCKG.Text+"     ";
                }
                if (cb_LoaiGo_BCKG.Text != "")
                {
                    GhiChu1 += " and LoaiGo='" + cb_LoaiGo_BCKG.Text + "'";
                    DVT1 += " Loại gỗ: " + cb_LoaiGo_BCKG.Text+"      ";
                }
                if (cb_LyDo_BCKG.Text != "")
                {
                    GhiChu1 += " and LyDo='" + prs.substring_right(cb_LyDo_BCKG.Text,child) + "'";
                    DVT1 += " Lý Do: " + prs.substring_left(cb_LyDo_BCKG.Text, child);
                }
                if (rd_Phach_BCKG.IsChecked == true)
                {
                    string_temp = @"select CDay,CRong,CDai,SUM(qry1.SLTonDau) as SLTonDau,SUM(qry1.SKTonDau) as SKTonDau,SUM(SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,SUM(SLTonDau)+SUM(SLNhap)-SUM(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon,ADorKD  from
                                    (SELECT [CDay],[CRong],[CDai],SLTonDau=0,SKTonDau=0,sum([SLNhap]) as SLNhap,sum([SLXuat]) as SLXuat,sum(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,sum(CDay*CRong*CDai*SLXuat)/1000000000 as SoKhoiXuat,ADorKD   
                                    FROM [dbo].[Nhap_Phach] inner join dbo.ChungTuNhap on ";
                    string_temp += "                (ChungTuNhap.Serial=Nhap_Phach.Serial_ChungTu";
              
                    string_temp += GhiChu1;
                    string_temp += " and ChungTuNhap.Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "') group by [CDay],[CRong],[CDai],ADorKD";
                    string_temp += @" union all 
                                    SELECT [CDay],[CRong],[CDai],SUM(SLNhap)-SUM(SLXuat) as SLTonDau,(sum(CDay*CRong*CDai*SLNhap)-sum(CDay*CRong*CDai*SLXuat))/1000000000 as SKTonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0,ADorKD
                                    from Nhap_Phach inner join dbo.ChungTuNhap on 
                                    (ChungTuNhap.Serial=Nhap_Phach.Serial_ChungTu ";
                    string_temp += GhiChu1;
                    string_temp += @" and ChungTuNhap.Ngay<'" + Datebegin + "') group by [CDay],[CRong],[CDai],ADorKD) as qry1 group by CDay,CRong,CDai,ADorKD";
                    
                }
                if (rd_Van_BCKG.IsChecked == true)
                {
                    string_temp = @"select CDay,SUM(qry1.SLTonDau) as SLTonDau,SUM(qry1.SKTonDau) as SKTonDau,SUM(SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,SUM(SLTonDau)+SUM(SLNhap)-SUM(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon,ADorKD  from
                                    (SELECT [CDay],SLTonDau=0,SKTonDau=0,sum([SLNhap]) as SLNhap,sum([SLXuat]) as SLXuat,sum(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,ADorKD  
                                    FROM [dbo].[Nhap_Van] inner join dbo.ChungTuNhap on 
                                    (ChungTuNhap.Serial=Nhap_Van.Serial_ChungTu ";
                   string_temp += GhiChu1;
                   string_temp += @"                 and ChungTuNhap.Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "') group by [CDay],ADorKD";
                   string_temp += @"                 union all 
                                    SELECT [CDay],SUM(SLNhap)-SUM(SLXuat) as SLTonDau,sum(SoKhoiNhap)-sum(SoKhoiXuat) as SKTonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0,ADorKD
                                    from Nhap_Van inner join dbo.ChungTuNhap on 
                                    (ChungTuNhap.Serial=Nhap_Van.Serial_ChungTu ";
                   string_temp += GhiChu1;
                   string_temp += @" and ChungTuNhap.Ngay<'" + Datebegin + "') group by [CDay],ADorKD)  as qry1 group by CDay,ADorKD";
                }
                if (rd_GoTron_BCKG.IsChecked == true)
                {
                    string_temp = @"select Vanh,CDai,SUM(qry1.SLTonDau) as SLTonDau,SUM(qry1.SKTonDau) as SKTonDau,SUM(SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,SUM(SLTonDau)+SUM(SLNhap)-SUM(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon,ADorKD  from
                                    (SELECT [CDai],Vanh,SLTonDau=0,SKTonDau=0,sum([SLNhap]) as SLNhap,sum([SLXuat]) as SLXuat,sum(CDai*Vanh*Vanh*SLNhap)/(4 * 3.1415926 * 1000000000) as SoKhoiNhap,sum(CDai*Vanh*Vanh*SLXuat)/(4 * 3.1415926 * 1000000000) as SoKhoiXuat,ADorKD
                                    FROM [dbo].[Nhap_GoTron] inner join dbo.ChungTuNhap on 
                                    (ChungTuNhap.Serial=Nhap_GoTron.Serial_ChungTu ";
                    string_temp += GhiChu1;
                    string_temp += @"                 and ChungTuNhap.Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "') group by [CDai],Vanh,ADorKD";
                   string_temp += @"                 union all 
                                    SELECT [CDai],Vanh,SUM(SLNhap)-SUM(SLXuat) as SLTonDau,(sum(CDai*Vanh*Vanh*SLNhap)-sum(CDai*Vanh*Vanh*SLXuat))/(4 * 3.1415926 * 1000000000) as SKTonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0,ADorKD
                                    from Nhap_GoTron inner join dbo.ChungTuNhap on 
                                    (ChungTuNhap.Serial=Nhap_GoTron.Serial_ChungTu ";
                   string_temp += GhiChu1;
                   string_temp += @"                 and ChungTuNhap.Ngay<'" + Datebegin + "') group by [CDai],Vanh,ADorKD) as qry1 group by CDai,Vanh,ADorKD";

                }
               // MessageBox.Show(string_temp);
                //MessageBox.Show(DVT1);
                ds = prs.dts(string_temp, Conn, ds);
                Conn.Close();
                return ds;
                /**/
           
        }
        private void bt_Search_BCKG_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = Report_KG();
                Fr_Report FrR = new Fr_Report();
                string_temp = "Từ ngày: " + dtp_Datebegin_BCKG.Text + " Đến ngày: " + dtp_Dateend_BCKG.Text;
                if (rd_Phach_BCKG.IsChecked == true || rd_Van_BCKG.IsChecked == true)
                    FrR.CrR_PhachSC_ReportKG(ds, string_temp, DVT1);
                if (rd_GoTron_BCKG.IsChecked == true)
                    FrR.CrR_GoTron_ReportKG(ds, string_temp, DVT1);
                // FrR.report_KV1NKTG(ds, Ngay1);
                FrR.Show();
                ds.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion
        #region //Bang Ke
        private DataSet BangKe_KG()
        {
            DataSet ds = new DataSet();
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1="";
            Ngay1="";
            Datebegin = Date_Split(dtp_Datebegin_BKKG.Text) + " 00:00";
            Dateend = Date_Split(dtp_Dateend_BKKG.Text) + " 23:59";
            if (rd_Phach_BKKG.IsChecked == true)
            {
                string_temp = @"select dbo.ChungTuNhap.Serial,dbo.ChungTuNhap.ChungTu,dbo.Nhap_Phach.CDay as DVD1,dbo.Nhap_Phach.CRong as DVD2,dbo.Nhap_Phach.CDai as DVD3,
                            dbo.Nhap_Phach.SLNhap,dbo.Nhap_Phach.SLXuat,dbo.Nhap_Phach.DVT,dbo.Nhap_Phach.CDai*CRong*CDay*SLNhap/1000000000 as SKNhap,dbo.Nhap_Phach.CDai*CRong*CDay*SLXuat/1000000000 as SKXuat,
                            dbo.Nhap_Phach.PhanLoai,dbo.ChungTuNhap.ADorKD,dbo.ChungTuNhap.LoaiGo,dbo.ChungTuNhap.NoiGiao,dbo.ChungTuNhap.NoiNhan,dbo.ChungTuNhap.LyDo,
                            dbo.ChungTuNhap.BienBan,dbo.ChungTuNhap.Ngay,dbo.Nhap_Phach.NgayInsert,ChungTuNhap.SoXe,dbo.Nhap_Phach.Wait
                            from dbo.Nhap_Phach inner join dbo.ChungTuNhap on (dbo.ChungTuNhap.Serial=dbo.Nhap_Phach.Serial_ChungTu";
                if (txt_DVD1_BKKG.Text != "")
                    GhiChu1 += " and CDay=" + txt_DVD1_BKKG.Text;
                if (txt_DVD2_BKKG.Text != "")
                    GhiChu1 += " and CRong=" + txt_DVD2_BKKG.Text;
                if (txt_DVD3_BKKG.Text != "")
                    GhiChu1 += " and CDai=" + txt_DVD3_BKKG.Text;
                if (chk_NgayCT.Checked == true)
                    Ngay1 += " and ChungTuNhap.Ngay>='" + Datebegin + "' and ChungTuNhap.Ngay<='" + Dateend + "'";
                else
                    Ngay1 += " and dbo.Nhap_Phach.NgayInsert>='" + Datebegin + "' and dbo.Nhap_Phach.NgayInsert<='" + Dateend + "'";
            }
            if (rd_Van_BKKG.IsChecked == true)
            {
                string_temp = @"select dbo.ChungTuNhap.Serial,dbo.ChungTuNhap.ChungTu,dbo.Nhap_Van.CDay as DVD1,'' as DVD2,'' as DVD3,
                                dbo.Nhap_Van.SLNhap,dbo.Nhap_Van.SLXuat,dbo.Nhap_Van.DVT,SoKhoiNhap as SKNhap,SoKhoiXuat as SKXuat,
                                '' as PhanLoai,dbo.ChungTuNhap.ADorKD,dbo.ChungTuNhap.LoaiGo,dbo.ChungTuNhap.NoiGiao,dbo.ChungTuNhap.NoiNhan,dbo.ChungTuNhap.LyDo,
                                dbo.ChungTuNhap.BienBan,convert(varchar(10),dbo.ChungTuNhap.Ngay,103) as Ngay,dbo.Nhap_Van.NgayInsert,ChungTuNhap.SoXe,dbo.Nhap_Van.Wait
                                from dbo.Nhap_Van inner join dbo.ChungTuNhap on (dbo.ChungTuNhap.Serial=dbo.Nhap_Van.Serial_ChungTu";
                if (txt_DVD1_BKKG.Text != "")
                    GhiChu1 += " and CDay=" + txt_DVD1_BKKG.Text;
                
               
                if (chk_NgayCT.Checked == true)
                    Ngay1 += " and ChungTuNhap.Ngay>='" + Datebegin + "' and ChungTuNhap.Ngay<='" + Dateend + "'";
                else
                    Ngay1 += " and dbo.Nhap_Van.NgayInsert>='" + Datebegin + "' and dbo.Nhap_Van.NgayInsert<='" + Dateend + "'";
  
            }
            if (rd_GoTron_BKKG.IsChecked == true)
            {
                string_temp = @"select dbo.ChungTuNhap.Serial,dbo.ChungTuNhap.ChungTu,dbo.Nhap_GoTron.CDai as DVD1,dbo.Nhap_GoTron.Vanh as DVD2,'' as DVD3,
                            dbo.Nhap_GoTron.SLNhap,dbo.Nhap_GoTron.SLXuat,dbo.Nhap_GoTron.DVT,(Vanh * Vanh * CDai * SLNhap) 
                                             / (4 * 3.1415926 * 1000000000) AS SKNhap, (Vanh * Vanh * CDai * SLXuat) / (4 * 3.1415926 * 1000000000) AS SKXuat,
                            '' as PhanLoai,dbo.ChungTuNhap.ADorKD,dbo.ChungTuNhap.LoaiGo,dbo.ChungTuNhap.NoiGiao,dbo.ChungTuNhap.NoiNhan,dbo.ChungTuNhap.LyDo,
                            dbo.ChungTuNhap.BienBan,convert(varchar(10),dbo.ChungTuNhap.Ngay,103) as Ngay,dbo.Nhap_GoTron.NgayInsert,ChungTuNhap.SoXe,dbo.Nhap_GoTron.Wait
                            from dbo.Nhap_GoTron inner join dbo.ChungTuNhap on (dbo.ChungTuNhap.Serial=dbo.Nhap_GoTron.Serial_ChungTu";
                if (txt_DVD1_BKKG.Text != "")
                    GhiChu1 += " and Dai=" + txt_DVD1_BKKG.Text;
                if (txt_DVD2_BKKG.Text != "")
                    GhiChu1 += " and Vanh=" + txt_DVD2_BKKG.Text;
                if (chk_NgayCT.Checked == true)
                    Ngay1 += " and ChungTuNhap.Ngay>='" + Datebegin + "' and ChungTuNhap.Ngay<='" + Dateend + "'";
                else
                    Ngay1 += " and dbo.Nhap_GoTron.NgayInsert>='" + Datebegin + "' and dbo.Nhap_GoTron.NgayInsert<='" + Dateend + "'";
            }
            if (cb_NoiGiao_BKKG.Text != "")
                GhiChu1 += " and NoiGiao='" + prs.substring_left(cb_NoiGiao_BKKG.Text,child) + "'";
            if (cb_NoiNhan_BKKG.Text != "")
                GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_BKKG.Text,child) + "'";
            if (cb_AdorKD_BKKG.Text != "")
                GhiChu1 += " and ADorKD='" + cb_AdorKD_BKKG.Text + "'";
            if (cb_LoaiGo_BKKG.Text != "")
                GhiChu1 += " and LoaiGo='" + cb_LoaiGo_BKKG.Text + "'";
            if (cb_LyDo_BKKG.Text != "")
                GhiChu1 += " and LyDo='" + prs.substring_right(cb_LyDo_BKKG.Text, child)+"'";
            if (txt_BienBan_BKKG.Text != "")
                GhiChu1 += " and BienBan='" + txt_BienBan_BKKG.Text + "'";
            if (txt_ChungTu_BKKG.Text != "")
                GhiChu1 += " and ChungTu='" + txt_ChungTu_BKKG.Text + "'";


            string_temp += GhiChu1 + Ngay1+")";
            ds = prs.dts(string_temp, Conn, ds);
            Conn.Close();
            return ds;
        }
        private void bt_Search_BKKG_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = BangKe_KG();
                radGridView6.DataSource = ds.Tables[0];
                SLNhap_d = 0;
                SLXuat_d = 0;
                SoKhoi_d = 0;
                temp_double = 0;
                for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    SLNhap_d += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                    SLXuat_d += double.Parse(ds.Tables[0].Rows[i]["SLXuat"].ToString());
                    SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SKNhap"].ToString());
                    temp_double += double.Parse(ds.Tables[0].Rows[i]["SKXuat"].ToString());

                }
                lb_SLNhap_BKKG.Text = string.Format("{0:###,###,###}", SLNhap_d);
                lb_SLXuat_BKKG.Text = string.Format("{0:###,###,###}", SLXuat_d);
                lb_SKNhap_BKKG.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                lb_SKXuat_BKKG.Text = string.Format("{0:###,###.###}", temp_double);
                if (ds.Tables[0].Rows.Count == 0)
                    MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra thông tin tìm kiếm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void rd_Van_BKKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView6.Columns["DVD1_BKKG"].HeaderText = "C Dày";
            radGridView6.Columns["DVD2_BKKG"].HeaderText = "";
            radGridView6.Columns["DVD3_BKKG"].HeaderText = "";
            lb_DVD1_BKKG.Text = "C Dày";
            lb_DVD2_BKKG.Text = "";
            lb_DVD3_BKKG.Text = "";
        }

        private void rd_Phach_BKKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView6.Columns["DVD1_BKKG"].HeaderText = "C Dày";
            radGridView6.Columns["DVD2_BKKG"].HeaderText = "C Rộng";
            radGridView6.Columns["DVD3_BKKG"].HeaderText = "C Dài";
            lb_DVD1_BKKG.Text = "C Dày";
            lb_DVD2_BKKG.Text = "C Rộng";
            lb_DVD3_BKKG.Text = "C Dài";
        }

        private void rd_GoTron_BKKG_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView6.Columns["DVD1_BKKG"].HeaderText = "C Dài";
            radGridView6.Columns["DVD2_BKKG"].HeaderText = "Vanh";
            radGridView6.Columns["DVD3_BKKG"].HeaderText = "";
            lb_DVD1_BKKG.Text = "C Dài";
            lb_DVD2_BKKG.Text = "Vanh";
            lb_DVD3_BKKG.Text = "";
        }
        private void exportexcel_BKKG()
        {
            if (radGridView6.Rows.Count > 0)
            {
                DataTable dt = (DataTable)radGridView6.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\KhoGo_BangKe.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }
        private void bt_exportexcel_BKKG_Click(object sender, EventArgs e)
        {
            exportexcel_BKKG();
        }
        #endregion
        #region// Nhap LoSay
        private void Checked_SoChePhach_inputLS()
        {
            cb_QuyCach_inputLS.Enabled = true;
            cb_PhanLoai_inputLS.Enabled = true;
            txt_SLPhach_inputLS.Enabled = true;
            txt_CDay_inputLS.Enabled = false;
            txt_SLVan_inputLS.Enabled = false;
            txt_SoKhoiVan_inputLS.Enabled = false;

            txt_CDaiGT_inputLS.Enabled = false;
            txt_VanhGT_inputLS.Enabled = false;
            txt_SLGT_inputLS.Enabled = false;
            radGridView7.Columns["DVD1_inputLS"].HeaderText = "C Dày";
            radGridView7.Columns["DVD2_inputLS"].HeaderText = "C Rộng";
            radGridView7.Columns["DVD3_inputLS"].HeaderText = "C Dài";


        }
        private void Checked_Van_inputLS()
        {
            cb_QuyCach_inputLS.Enabled = false;
            cb_PhanLoai_inputLS.Enabled = false;
            txt_SLPhach_inputLS.Enabled = false;
            txt_CDay_inputLS.Enabled = true;
            txt_SLVan_inputLS.Enabled = true;
            txt_SoKhoiVan_inputLS.Enabled = true;

            txt_CDaiGT_inputLS.Enabled = false;
            txt_VanhGT_inputLS.Enabled = false;
            txt_SLGT_inputLS.Enabled = false;
            radGridView7.Columns["DVD1_inputLS"].HeaderText = "C Dày";
            radGridView7.Columns["DVD2_inputLS"].HeaderText = " ";
            radGridView7.Columns["DVD3_inputLS"].HeaderText = " ";

        }
        private void Checked_GoTron_inputLS()
        {
            cb_QuyCach_inputLS.Enabled = false;
            cb_PhanLoai_inputLS.Enabled = false;
            txt_SLPhach_inputLS.Enabled = false;
            txt_CDay_inputLS.Enabled = false;
            txt_SLVan_inputLS.Enabled = false;
            txt_SoKhoiVan_inputLS.Enabled = false;

            txt_CDaiGT_inputLS.Enabled = true;
            txt_VanhGT_inputLS.Enabled = true;
            txt_SLGT_inputLS.Enabled = true;
            radGridView7.Columns["DVD2_inputLS"].HeaderText = "C Dài";
            radGridView7.Columns["DVD1_inputLS"].HeaderText = "Vanh";
            radGridView7.Columns["DVD3_inputLS"].HeaderText = " ";

        }
        private void rd_Phach_inputLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_SoChePhach_inputLS();
        }
        private void cb_QuyCach_inputLS_Leave(object sender, EventArgs e)
        {
            try
            {
                if (cb_QuyCach_inputLS.Text != "")
                {
                    string_temp = prs.substring_right(cb_QuyCach_inputLS.Text, child);
                    string[] temp = string_temp.Split('x');
                    DVD1 = temp[0].ToString().Trim();
                    DVD2 = temp[1].ToString().Trim();
                    DVD3 = temp[2].ToString().Trim();
                    temp.Clone();
                    if (DVD1 == "" || DVD2 == "" || DVD3 == "")
                    {
                        cb_QuyCach_inputLS.Text = "";
                        MessageBox.Show("Bạn đang nhập sai quy cách");
                        cb_QuyCach_inputLS.Focus();
                    }
                   
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void rd_Van_inputLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_Van_inputLS();
        }

        private void rd_GoTron_inputLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            Checked_GoTron_inputLS();
        }
        private void main_info_DisableinputLS()
        {
            cb_NoiGiao_inputLS.Enabled = false;
            cb_NoiNhan_inputLS.Enabled = false;
            cb_AdorKD_inputLS.Enabled = false;
            cb_LyDo_inputLS.Enabled = false;
            txt_BienBan_inputLS.Enabled = false;
            txt_ChungTu_inputLS.Enabled = false;
            //txt_SoXe_inputLS.Enabled = false;
            cb_NguonGo_inputLS.Enabled = false;
        }
        private void main_info_EnableinputLS()
        {
            cb_NoiGiao_inputLS.Enabled = true;
            cb_NoiNhan_inputLS.Enabled = true;
            cb_AdorKD_inputLS.Enabled = true;
            cb_LyDo_inputLS.Enabled = true;
            txt_BienBan_inputLS.Enabled = true;
            txt_ChungTu_inputLS.Enabled = true;
            // txt_SoXe_inputLS.Enabled = true;
            cb_NguonGo_inputLS.Enabled = true;
            rd_GoTron_inputLS.Enabled = true;
            rd_Van_inputLS.Enabled = true;
            rd_Phach_inputLS.Enabled = true;
        }
        private void check_main_infoinputLS()
        {
            try
            {
                NoiGiao1 = cb_NoiGiao_inputLS.Text;
                NoiNhan1 = cb_NoiNhan_inputLS.Text;
                count = 0;
                for (i = 0; i < cb_NoiNhan_inputLS.Items.Count; i++)
                {
                    if (NoiGiao1 == cb_NoiNhan_inputLS.Items[i].ToString())
                        count++;
                    if (NoiNhan1 == cb_NoiNhan_inputLS.Items[i].ToString())
                        count++;
                    if (count == 2)
                        break;

                }
                if (count < 2 || (cb_AdorKD_inputLS.Text != "AD" && cb_AdorKD_inputLS.Text != "KD"))
                {
                    MessageBox.Show("vui lòng kiểm tra lại thông tin NƠI GIAO hoặc LÒ SẤY");
                    cb_NoiGiao_inputLS.Text = "";

                    cb_NoiGiao_inputLS.Focus();
                }
                else
                {
                    LyDo1 = cb_LyDo_inputLS.Text;
                    for (i = 0; i < cb_LyDo_inputLS.Items.Count; i++)
                    {
                        if (LyDo1 == cb_LyDo_inputLS.Items[i].Text)
                        {
                            count++;
                            break;
                        }
                    }
                    if (count < 3)
                    {
                        MessageBox.Show("Kiểm tra lại lý do nhập");
                        cb_LyDo_inputLS.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void begin_load_inputLS()
        {
            //rd_Van_inputLS.Enabled = false;
            txt_CDay_inputLS.Enabled = false;
            txt_SLVan_inputLS.Enabled = false;
            txt_SoKhoiVan_inputLS.Enabled = false;
            // rd_GoTron_inputLS.Enabled = false;
            txt_CDaiGT_inputLS.Enabled = false;
            txt_VanhGT_inputLS.Enabled = false;
            txt_SLGT_inputLS.Enabled = false;
        }
        private void Addgrid_inputLS()
        {
            try
            {
                
                DVT1 = cb_DVT_inputLS.Text.Trim();
                KhachHang1 = cb_KhachHang_inputLS.Text.Trim();
                GhiChu1 = txt_GhiChu_inputLS.Text.Trim();
                count = 0;
                /*NoiGiao1 = prs.substring_left(cb_NoiGiao_inputLS.Text, child);
                NoiNhan1 = prs.substring_left(cb_NoiNhan_inputLS.Text, child);
                ADorKD1 = cb_AdorKD_inputLS.Text.Trim();
                PhanLoai1 = cb_PhanLoai_inputLS.Text.Trim();
                LyDo1 = prs.substring_right(cb_LoaiGo_inputLS.Text, child);
                BienBan1 = txt_BienBan_inputLS.Text.Trim();
                ChungTu1 = txt_BienBan_inputLS.Text.Trim();
                Ngay1 = Date_Split(dtp_Date_inputLS.Text);
                SoXe1 = txt_SoXe_inputLS.Text.Trim();
                ChungTu1 = txt_ChungTu_inputLS.Text.Trim();
                 */
                if (rd_Phach_inputLS.IsChecked == true)
                {
                    for (i = 0; i < cb_QuyCach_inputLS.Items.Count; i++)
                    {
                        if (cb_QuyCach_inputLS.Text == cb_QuyCach_inputLS.Items[i].ToString())
                        {
                            count++;
                            break;
                        }
                    }
                    if (count == 0)
                        MessageBox.Show("Nhập sai quy cách");
                    else
                    {
                        txt_SLPhach_inputLS_Leave(null, null);
                        string_temp = prs.substring_right(cb_QuyCach_inputLS.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();
                        PhanLoai1 = cb_PhanLoai_inputLS.Text.Trim();
                        SoLuong1 = txt_SLPhach_inputLS.Text.Trim();
                        temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                        SoKhoi1 = temp_double.ToString();

                    }
                }
                if (rd_Van_inputLS.IsChecked == true)
                {
                    txt_SLVan_inputLS_Leave(null, null);
                    DVD1 = txt_CDay_inputLS.Text.Trim();
                    SoLuong1 = txt_SLVan_inputLS.Text.Trim();
                    SoKhoi1 = txt_SoKhoiVan_inputLS.Text.Trim();
                    if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        DVD2 = "";
                        DVD3 = "";
                        count = 1;
                    }

                }
                if (rd_GoTron_inputLS.IsChecked == true)
                {

                    DVD1 = txt_VanhGT_inputLS.Text.Trim();
                    DVD2 = txt_CDaiGT_inputLS.Text.Trim();
                    SoLuong1 = txt_SLGT_inputLS.Text.Trim();

                    if (DVD1 == "" || DVD2 == "" || SoLuong1 == "")
                        MessageBox.Show("Nhập thiếu thông tin");
                    else
                    {
                        temp_double = double.Parse(DVD1) * double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(SoLuong1) / (4 * 3.1415926 * 1000000000);
                        SoKhoi1 = temp_double.ToString();
                        count = 1;
                        DVD3 = "";
                    }
                }
                if (count == 1)
                {
                    int_temp = radGridView7.Rows.Count;
                    if (int_temp > 0)
                    {
                        i=int.Parse(radGridView7.Rows[int_temp-1].Cells["STT_inputLS"].Value.ToString())+1;
                        radGridView7.Rows.Add(i.ToString(),DVD1, DVD2, DVD3, SoLuong1, SoKhoi1, PhanLoai1, DVT1, KhachHang1, GhiChu1, "", "","");
                    }
                    else
                        radGridView7.Rows.Add("1",DVD1, DVD2, DVD3, SoLuong1, SoKhoi1, PhanLoai1, DVT1, KhachHang1, GhiChu1, "", "","");
                    //temp_double=double.Parse(
                    cb_QuyCach_inputLS.Text = "";
                    cb_PhanLoai_inputLS.Text = "";
                    txt_SLPhach_inputLS.Text = "";
                    txt_CDay_inputLS.Text = "";
                    txt_SLVan_inputLS.Text = "";
                    txt_SoKhoiVan_inputLS.Text = "";
                    txt_CDaiGT_inputLS.Text = "";
                    txt_VanhGT_inputLS.Text = "";
                    txt_SLGT_inputLS.Text = "";
                    txt_QC2_inputLS.Text = "";
                    txt_QC1_inputLS.Text = "";
                    txt_QC3_inputLS.Text = "";
                    if (cb_NoiGiao_inputLS.Enabled == true)
                    {
                        check_main_infoinputLS();
                        //main_info_DisableinputLS();
                        if (rd_Phach_inputLS.IsChecked == true)
                        {
                            rd_GoTron_inputLS.Enabled = false;
                            rd_Van_inputLS.Enabled = false;
                            txt_CDay_inputLS.Enabled = false;
                            txt_SLVan_inputLS.Enabled = false;
                            txt_SoKhoiVan_inputLS.Enabled = false;
                            txt_CDaiGT_inputLS.Enabled = false;
                            txt_VanhGT_inputLS.Enabled = false;
                            txt_SLGT_inputLS.Enabled = false;
                            txt_QC1_inputLS.Focus();
                        }
                        if (rd_Van_inputLS.IsChecked == true)
                        {
                            rd_GoTron_inputLS.Enabled = false;
                            rd_Phach_inputLS.Enabled = false;
                            txt_CDaiGT_inputLS.Enabled = false;
                            txt_VanhGT_inputLS.Enabled = false;
                            txt_SLGT_inputLS.Enabled = false;
                            cb_QuyCach_inputLS.Enabled = false;
                            txt_SLPhach_inputLS.Enabled = false;
                            cb_PhanLoai_inputLS.Enabled = false;
                            txt_CDay_inputLS.Focus();
                        }
                        if (rd_GoTron_inputLS.IsChecked == true)
                        {
                            rd_Van_inputLS.Enabled = false;
                            rd_Phach_inputLS.Enabled = false;
                            cb_QuyCach_inputLS.Enabled = false;
                            txt_SLPhach_inputLS.Enabled = false;
                            cb_PhanLoai_inputLS.Enabled = false;
                            txt_CDay_inputLS.Enabled = false;
                            txt_SLVan_inputLS.Enabled = false;
                            txt_SoKhoiVan_inputLS.Enabled = false;
                            txt_VanhGT_inputLS.Focus();
                        }
                    }
                    if (rd_Phach_inputLS.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_inputLS.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_inputLS.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_inputLS.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_inputLS.Text = string.Format("{0:###,###,###}", temp_double);
                        txt_QC1_inputLS.Focus();
                    }
                    if (rd_Van_inputLS.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_inputLS.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_inputLS.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_inputLS.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_inputLS.Text = string.Format("{0:###,###,###.#}", temp_double);
                        txt_CDay_inputLS.Focus();
                    }
                    if (rd_GoTron_inputLS.IsChecked == true)
                    {
                        temp_double = double.Parse(lb_TongKhoi_inputLS.Text);
                        temp_double += double.Parse(SoKhoi1);
                        lb_TongKhoi_inputLS.Text = temp_double.ToString();
                        temp_double = double.Parse(lb_TongSL_inputLS.Text);
                        temp_double += double.Parse(SoLuong1);
                        lb_TongSL_inputLS.Text = temp_double.ToString();
                        txt_VanhGT_inputLS.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void dtp_Date_inputLS_Leave(object sender, EventArgs e)
        {

        }
        private void bt_add_inputLS_Click(object sender, EventArgs e)
        {
            Addgrid_inputLS();
        }

        private void radGridView7_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView7.Rows.Count > 0)
            {
                try
                {
                    //MessageBox.Show(radGridView7.Rows.Count.ToString());
                    int r = radGridView7.CurrentCell.RowIndex;

                    temp_double = double.Parse(lb_TongSL_inputLS.Text) - double.Parse(radGridView7.Rows[r].Cells["SoLuong_inputLS"].Value.ToString());
                    lb_TongSL_inputLS.Text = string.Format("{0:###,###}", temp_double);
                    temp_double = double.Parse(lb_TongKhoi_inputLS.Text) - double.Parse(radGridView7.Rows[r].Cells["SoKhoi_inputLS"].Value.ToString());
                    lb_TongKhoi_inputLS.Text = string.Format("{0:###,###.####}", temp_double);
                    radGridView7.Rows.RemoveAt(r);

                    j = radGridView7.Rows.Count - 1;
                    //radGridView7.CurrentCell = radGridView7.Rows[j].Cells["MaCTinput"];

                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (radGridView7.Rows.Count == 0)
            {
                lb_TongKhoi_inputLS.Text = "0";
                lb_TongSL_inputLS.Text = "0";
            }
        }
        /*private void print_inputLS()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Datebegin_inputLS.Text);
                if (rd_Phach_inputLS.IsChecked == true)
                {
                    string_temp = @" select dbo.ChungTuLoSay.Serial,ChungTuLoSay.NoiGiao,ChungTuLoSay.NoiNhan,ChungTuLoSay.ADorKD,ChungTuLoSay.BienBan as SoBB,ChungTuLoSay.ChungTu,ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.Serial,convert(varchar(10),ChungTuLoSay.Ngay,103) as Ngay
                                ,dbo.Nhap_PhachLS.CDai,Nhap_PhachLS.CDay,Nhap_PhachLS.CRong,Nhap_PhachLS.KhachHang,Nhap_PhachLS.SLNhap,(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,Nhap_PhachLS.SLXuat,(CDay*CRong*CDai*SLXuat)/1000000000 as SoKhoiXuat,Nhap_PhachLS.PhanLoai
                                from dbo.Nhap_Phach inner join dbo.ChungTuLoSay on (dbo.Nhap_PhachLS.Serial_ChungTu=ChungTuLoSay.Serial";
                    if (cb_QuyCach_inputLS.Text != "")
                    {
                        string_temp = prs.substring_right(cb_QuyCach_inputLS.Text, child);
                        string[] temp = string_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();

                        GhiChu1 += " and Nhap_PhachLS.CDay=" + DVD1 + " and Nhap_PhachLS.CRong=" + DVD2 + " and Nhap_PhachLS.CDai=" + DVD3;
                    }
                    if (cb_PhanLoai_inputLS.Text != "")
                        GhiChu1 += "and Nhap_PhachLS.PhanLoai='" + cb_PhanLoai_inputLS.Text + "'";
                }
                if (rd_Van_inputLS.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuLoSay.Serial,ChungTuLoSay.NoiGiao,ChungTuLoSay.NoiNhan,ChungTuLoSay.ADorKD,ChungTuLoSay.BienBan as SoBB,ChungTuLoSay.ChungTu,ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.Serial,convert(varchar(10),ChungTuLoSay.Ngay,103) as Ngay
                                ,Nhap_VanLS.CDay,Nhap_VanLS.KhachHang,Nhap_VanLS.SLNhap,SoKhoiNhap,Nhap_VanLS.SLXuat,SoKhoiXuat
                                from dbo.Nhap_Van inner join dbo.ChungTuLoSay on (dbo.Nhap_VanLS.Serial_ChungTu=ChungTuLoSay.Serial";
                    if (txt_CDay_inputLS.Text != "")
                        GhiChu1 += " and Nhap_VanLS.CDay=" + txt_CDay_inputLS.Text;
                }
                if (rd_GoTron_inputLS.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuLoSay.Serial,ChungTuLoSay.NoiGiao,ChungTuLoSay.NoiNhan,ChungTuLoSay.ADorKD,ChungTuLoSay.BienBan as SoBB,ChungTuLoSay.ChungTu,ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.Serial,convert(varchar(10),ChungTuLoSay.Ngay,103) as Ngay
                                ,dbo.Nhap_GoTronLS.CDai,Nhap_GoTronLS.Vanh,Nhap_GoTronLS.KhachHang,Nhap_GoTronLS.SLNhap,(Vanh*Vanh*CDai*SLNhap)/(4*3.141593*1000000000) as SoKhoiNhap,Nhap_GoTronLS.SLXuat,(Vanh*Vanh*CDai*SLXuat)/(4*3.141593*1000000000) as SoKhoiXuat
                                from dbo.Nhap_GoTron inner join dbo.ChungTuLoSay on (dbo.Nhap_GoTronLS.Serial_ChungTu=ChungTuLoSay.Serial";
                    if (txt_VanhGT_inputLS.Text != "")
                        GhiChu1 += " and Nhap_GoTronLS.Vanh=" + txt_VanhGT_inputLS.Text;
                    if (txt_CDaiGT_inputLS.Text != "")
                        GhiChu1 += " and Nhap_GoTronLS.CDai=" + txt_CDaiGT_inputLS.Text;
                }
                if (cb_NoiGiao_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.NoiGiao='" + prs.substring_left(cb_NoiGiao_inputLS.Text, child) + "'";
                if (cb_NoiNhan_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.NoiNhan='" + prs.substring_left(cb_NoiNhan_inputLS.Text, child) + "'";
                if (cb_LoaiGo_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.LoaiGo='" + cb_LoaiGo_inputLS.Text + "'";
                if (cb_LyDo_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.LyDo='" + prs.substring_right(cb_LyDo_inputLS.Text, child) + "'";
                if (txt_ChungTu_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.ChungTu='" + txt_ChungTu_inputLS.Text + "'";
                if (txt_BienBan_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.BienBan='" + txt_BienBan_inputLS.Text + "'";
                if (cb_AdorKD_inputLS.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_inputLS.Text + "'";
                string_temp += GhiChu1 + " and SLNhap<>0 and ChungTuLoSay.Ngay='" + Ngay1 + "')";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Datebegin_inputLS.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);

                if (rd_Phach_inputLS.IsChecked == true || rd_Van_inputLS.IsChecked == true)
                {
                    FrR.CrR_NK_PhachSC(ds, Ngay1);
                }
                if (rd_GoTron_inputLS.IsChecked == true)
                    FrR.CrR_NK_GoTron(ds, Ngay1);

                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }*/
        private void cb_LyDo_inputLS_Leave(object sender, EventArgs e)
        {
            check_main_infoinputLS();
        }
        private void save_inputLS()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Tran_Ins_CTNhapLS";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;

            try
            {

                //string insert_NhapKTam = null;
                if (lb_Serial_addinputLS.Text == "0" && (cb_NoiGiao_inputLS.Text == "" || cb_NoiNhan_inputLS.Text == "" || cb_AdorKD_inputLS.Text == "" || cb_NguonGo_inputLS.Text == "" || cb_LyDo_inputLS.Text == "" || txt_BienBan_inputLS.Text == "" || txt_ChungTu_inputLS.Text == "" || radGridView7.Rows.Count == 0))
                    MessageBox.Show("Nhập thiếu thông tin");
                else
                {
                    ADorKD1 = cb_AdorKD_inputLS.Text.Trim();
                    LoaiGo1 = cb_NguonGo_inputLS.Text.Trim();
                    BienBan1 = txt_BienBan_inputLS.Text.Trim();
                    ChungTu1 = txt_ChungTu_inputLS.Text.Trim();
                    Datebegin = Date_Split(dtp_Datebegin_inputLS.Text);
                    Dateend = Date_Split(dtp_Dateend_inputLS.Text);
                    if (lb_Serial_addinputLS.Text == "0" || lb_Serial_addinputLS.Visible == false)
                    {
                        NoiGiao1 = prs.substring_left(cb_NoiGiao_inputLS.Text, child);
                        NoiNhan1 = prs.substring_left(cb_NoiNhan_inputLS.Text, child);
                        
                        LyDo1 = prs.substring_right(cb_LyDo_inputLS.Text, child);
                        
                        //SoXe1 = txt_SoXe_inputLS.Text.Trim();
                        //Ngay1 = Date_Split(dtp_Datebegin_inputLS.Text);
                        
                        cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                        cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                        cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                        cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                        cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                        //cmd.Parameters.AddWithValue("@SoXe", SoXe1);
                        cmd.Parameters.AddWithValue("@NgayBegin", Datebegin);
                        cmd.Parameters.AddWithValue("@NgayEnd", Dateend);
                        cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                        SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                        code.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(code);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                        sdr.Close();
                        cmd.Parameters.Clear();
                    }
                    else
                        Serial1 = lb_Serial_addinputLS.Text;
                    cmd.CommandText = "Tran_Ins_CTNhap";
                    cmd.Parameters.AddWithValue("@NoiGiao", "CO1");
                    cmd.Parameters.AddWithValue("@NoiNhan", "Lo Say");
                    cmd.Parameters.AddWithValue("@ADorKD", "AD");
                    cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                    cmd.Parameters.AddWithValue("@LyDo", "GNLoSay");
                    cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                    cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                    cmd.Parameters.AddWithValue("@SoXe", "");
                    cmd.Parameters.AddWithValue("@Ngay", Datebegin);
                    cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                    SqlParameter code1 = new SqlParameter("@Serial", SqlDbType.Int);
                    code1.Direction = ParameterDirection.Output;
                    cmd.Parameters.Add(code1);
                    SqlDataReader sdr1 = cmd.ExecuteReader();
                    ID1 = cmd.Parameters["@Serial"].Value.ToString();
                    sdr1.Close();
                    cmd.Parameters.Clear();
                    string_temp = "select max(Serial) from dbo.Nhap_Phach";
                    SqlCommand cmd1 = new SqlCommand();
                    cmd1.Connection = Conn;
                    cmd1.CommandText = string_temp;
                    int_temp = int.Parse(cmd1.ExecuteScalar().ToString());
                    //int_temp = int.Parse(ID1);
                    cmd1.Dispose();

                    //MessageBox.Show("aaaa");
                    if (rd_Phach_inputLS.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapPhachLS";
                        i = 0;
                        while (radGridView7.Rows.Count>0)
                        {
                            DVD1 = radGridView7.Rows[i].Cells["DVD1_inputLS"].Value.ToString();
                            DVD2 = radGridView7.Rows[i].Cells["DVD2_inputLS"].Value.ToString();
                            DVD3 = radGridView7.Rows[i].Cells["DVD3_inputLS"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView7.Rows[i].Cells["SoLuong_inputLS"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView7.Rows[i].Cells["SoKhoi_inputLS"].Value.ToString();
                            PhanLoai1 = radGridView7.Rows[i].Cells["PhanLoai_inputLS"].Value.ToString();
                            DVT1 = radGridView7.Rows[i].Cells["DVT_inputLS"].Value.ToString();
                            KhachHang1 = radGridView7.Rows[i].Cells["KhachHang_inputLS"].Value.ToString();
                            GhiChu1 = radGridView7.Rows[i].Cells["GhiChu_inputLS"].Value.ToString();
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@Serial_ChungTuKG", ID1);
                            cmd.Parameters.AddWithValue("@Serial_LS", int_temp);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.Parameters.AddWithValue("@Wait", "1");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();

                            radGridView7.Rows.RemoveAt(i);
                            int_temp++;
                        }
                    }

                    if (rd_Van_inputLS.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapVanLS";
                        i = 0;
                        while (radGridView7.Rows.Count > 0)
                        {
                            DVD1 = radGridView7.Rows[i].Cells["DVD1_inputLS"].Value.ToString();
                            // DVD2 = radGridView7.Rows[i].Cells["DVT2_inputLS"].Value.ToString();
                            //DVD3 = radGridView7.Rows[i].Cells["DVT3_inputLS"].Value.ToString();
                            SoLuong1 = double.Parse(radGridView7.Rows[i].Cells["SoLuong_inputLS"].Value.ToString()).ToString();
                            SoKhoi1 = radGridView7.Rows[i].Cells["SoKhoi_inputLS"].Value.ToString();
                            //PhanLoai1 = radGridView7.Rows[i].Cells["PhanLoai_inputLS"].Value.ToString();
                            DVT1 = radGridView7.Rows[i].Cells["DVT_inputLS"].Value.ToString();
                            KhachHang1 = radGridView7.Rows[i].Cells["KhachHang_inputLS"].Value.ToString();
                            GhiChu1 = radGridView7.Rows[i].Cells["GhiChu_inputLS"].Value.ToString();
                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@Serial_ChungTuKG", ID1);
                            cmd.Parameters.AddWithValue("@Serial_LS", int_temp);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", SoKhoi1);
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                            cmd.Parameters.AddWithValue("@Wait", "1");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView7.Rows.RemoveAt(i);
                            int_temp++;
                        }


                    }
                    if (rd_GoTron_inputLS.IsChecked == true)
                    {
                        cmd.CommandText = "Trans_InsNhapGTLS";
                        i = 0;
                        while (radGridView7.Rows.Count > 0)
                        {
                            DVD1 = double.Parse(radGridView7.Rows[i].Cells["DVD1_inputLS"].Value.ToString()).ToString();
                            DVD2 = double.Parse(radGridView7.Rows[i].Cells["DVD2_inputLS"].Value.ToString()).ToString();
                            //DVD3 = radGridView7.Rows[i].Cells["DVT3_inputLS"].Value.ToString();
                            SoLuong1 = SoLuong1 = double.Parse(radGridView7.Rows[i].Cells["SoLuong_inputLS"].Value.ToString()).ToString();
                            //SoKhoi1 = radGridView7.Rows[i].Cells["SoKhoi_inputLS"].Value.ToString();
                            //PhanLoai1 = radGridView7.Rows[i].Cells["PhanLoai_inputLS"].Value.ToString();
                            DVT1 = radGridView7.Rows[i].Cells["DVT_inputLS"].Value.ToString();
                            KhachHang1 = radGridView7.Rows[i].Cells["KhachHang_inputLS"].Value.ToString();
                            GhiChu1 = radGridView7.Rows[i].Cells["GhiChu_inputLS"].Value.ToString();


                            cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@Serial_ChungTuKG", ID1);
                            cmd.Parameters.AddWithValue("@Serial_LS", int_temp);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                            cmd.Parameters.AddWithValue("@Wait", "1");
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            radGridView7.Rows.RemoveAt(i);
                            int_temp++;
                        }
                    }
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    bt_rework_inputLS_Click(null, null);
                }
            }

            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }

        }

        private void txt_CDay_inputLS_Leave(object sender, EventArgs e)
        {
            if (txt_CDay_inputLS.Text != "")
            {
                prs.checktext_number(txt_CDay_inputLS);
                prs.checktext_number(txt_CDay_inputLS);
                Conn = prs.Connect();
                SqlCommand cmd = new SqlCommand();
               
                try
                {
                    Conn.Open();
                    cmd.Connection = Conn;
                    cmd.CommandText = @"select SoKhoiTon from dbo.KhoGo_TK where DVD1="+txt_CDay_inputLS.Text+" and DVD2=0 and ADorKD='"+cb_AdorKD_inputLS.Text+"'";
                    lb_TonKhoKG.Text = cmd.ExecuteScalar().ToString();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    lb_TonKhoKG.Text = "0";
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }

            }
        }

        private void txt_SLPhach_inputLS_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLPhach_inputLS);
            try
            {
                string_temp = prs.substring_right(cb_QuyCach_inputLS.Text, child);
                string[] temp = string_temp.Split('x');
                DVD1 = temp[0].ToString().Trim();
                DVD2 = temp[1].ToString().Trim();
                DVD3 = temp[2].ToString().Trim();
                temp.Clone();
                SoKhoi_d = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(txt_SLPhach_inputLS.Text) / 1000000000;
                if (SoKhoi_d > double.Parse(lb_TonKhoKG.Text))
                {

                    MessageBox.Show("Số khối đã vượt quá số khối tồn kho, số lượng cho phép <" + string.Format("{0:###,###,###}", (double.Parse(lb_TonKhoKG.Text) * 1000000000 / (double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3)))));
                    //txt_SLPhach_inputLS.Text = "";
                    txt_SLPhach_inputLS.Focus();
                }
            }
            catch
            {
            }
        }

        private void txt_SLVan_inputLS_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLVan_inputLS);
        }

        private void txt_SoKhoiVan_inputLS_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SoKhoiVan_inputLS);
            try
            {
                if (double.Parse(txt_SoKhoiVan_inputLS.Text) > double.Parse(lb_TonKhoKG.Text))
                {
                    MessageBox.Show("Số khối nhập đã vượt quá số khối tồn kho");
                    //txt_SoKhoiVan_inputLS.Text = "";
                    txt_SoKhoiVan_inputLS.Focus();
                }
            }
            catch
            {
            }

        }

        private void txt_CDaiGT_inputLS_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_CDaiGT_inputLS);
        }

        private void txt_VanhGT_inputLS_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_VanhGT_inputLS);
        }

        private void txt_SLGT_inputLS_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SLGT_inputLS);
        }
        private void bt_save_inputLS_Click(object sender, EventArgs e)
        {
            save_inputLS();
        }

        private void cb_AdorKD_inputLS_Leave(object sender, EventArgs e)
        {
            if (cb_AdorKD_inputLS.Text != "AD" && cb_AdorKD_inputLS.Text != "KD")
            {
                MessageBox.Show("Nhập sai thông tin");
                cb_AdorKD_inputLS.Text = "";
                cb_AdorKD_inputLS.Focus();
            }
        }

        private void bt_rework_inputLS_Click(object sender, EventArgs e)
        {
            main_info_EnableinputLS();
            prs.reset_page_txt(Page_LoSayinput);
            cb_NoiGiao_inputLS.Focus();
            if (radGridView7.Rows.Count > 0)
            {
                for (i = radGridView7.Rows.Count - 1; i >= 0; i--)
                {
                    radGridView7.Rows.RemoveAt(i);
                }

            }
            rd_GoTron_inputLS.Enabled = true;
            rd_Phach_inputLS.Enabled = true;
            rd_Van_inputLS.Enabled = true;
            lb_Serial_input.Text = "0";
            lb_TongKhoi_inputLS.Text = "0";
            lb_TongSL_inputLS.Text = "0";
            lb_Serial_addinputLS.Text = "0";
            lb_CapNhat_inputLS.Visible = false;
            lb_Serial_addinputLS.Visible = false;
            bt_edit_inputLS.Enabled = false;
            bt_delete_inputLS.Enabled = false;
            bt_save_inputLS.Enabled = true;
            lb_Serial_LS.Text = "0";
        }
        private void Display_inputLS(int row)
        {
            //cb_NoiGiao_inputLS.Text = radGridView7.Rows[i].Cells["NoiGiao"].Value.ToString();

            if (radGridView7.Rows[row].Cells["Nhom_input"].Value.ToString() != "" && radGridView7.Rows[row].Cells["Wait_input"].Value.ToString() != "2")
            {

                //MessageBox.Show(radGridView7.Rows[row].Cells["Nhom_input"].Value.ToString());
                if (radGridView7.Rows[row].Cells["Nhom_input"].Value.ToString() == "Phach")
                {
                    rd_Phach_inputLS.Enabled = true;
                    rd_Phach_inputLS.IsChecked = true;
                    rd_Van_inputLS.Enabled = false;
                    rd_GoTron_inputLS.Enabled = false;
                    cb_QuyCach_inputLS.Text = radGridView7.Rows[row].Cells["DVD1_inputLS"].Value.ToString() + radGridView7.Rows[row].Cells["DVD2_inputLS"].Value.ToString() + radGridView7.Rows[row].Cells["DVD3_inputLS"].Value.ToString() + " - " + radGridView7.Rows[row].Cells["DVD1_inputLS"].Value.ToString() + "x" + radGridView7.Rows[row].Cells["DVD2_inputLS"].Value.ToString() + "x" + radGridView7.Rows[row].Cells["DVD3_inputLS"].Value.ToString();
                    cb_PhanLoai_inputLS.Text = radGridView7.Rows[row].Cells["PhanLoai_inputLS"].Value.ToString();
                    txt_SLPhach_inputLS.Text = radGridView7.Rows[row].Cells["SoLuong_inputLS"].Value.ToString();
                    txt_GhiChu_inputLS.Text = radGridView7.Rows[row].Cells["GhiChu_inputLS"].Value.ToString();
                   

                }
                if (radGridView7.Rows[row].Cells["Nhom_input"].Value.ToString() == "Van")
                {
                    rd_Van_inputLS.Enabled = true;
                    rd_Van_inputLS.IsChecked = true;
                    rd_Phach_inputLS.Enabled = false;
                    rd_GoTron_inputLS.Enabled = false;
                    txt_CDay_inputLS.Text = radGridView7.Rows[row].Cells["DVD1_inputLS"].Value.ToString();
                    txt_SLVan_inputLS.Text = radGridView7.Rows[row].Cells["SoLuong_inputLS"].Value.ToString();
                    txt_SoKhoiVan_inputLS.Text = radGridView7.Rows[row].Cells["SoKhoi_inputLS"].Value.ToString();
                    txt_GhiChu_inputLS.Text = radGridView7.Rows[row].Cells["GhiChu_inputLS"].Value.ToString();
                }
                if (radGridView7.Rows[row].Cells["Nhom_input"].Value.ToString() == "GoTron")
                {
                    rd_GoTron_inputLS.Enabled = true;
                    rd_GoTron_inputLS.IsChecked = true;
                    rd_Phach_inputLS.Enabled = false;
                    rd_Van_inputLS.Enabled = false;
                    txt_CDaiGT_inputLS.Text = radGridView7.Rows[row].Cells["DVD2_inputLS"].Value.ToString();
                    txt_VanhGT_inputLS.Text = radGridView7.Rows[row].Cells["DVD1_inputLS"].Value.ToString();
                    txt_SLGT_inputLS.Text = radGridView7.Rows[row].Cells["SoLuong_inputLS"].Value.ToString();
                    txt_GhiChu_inputLS.Text = radGridView7.Rows[row].Cells["GhiChu_inputLS"].Value.ToString();
                }
                lb_Serial_LS.Text = radGridView7.Rows[row].Cells["Serial_LSKG"].Value.ToString();
                cb_DVT_inputLS.Text = radGridView7.Rows[row].Cells["DVT_inputLS"].Value.ToString();
                cb_KhachHang_inputLS.Text = radGridView7.Rows[row].Cells["KhachHang_inputLS"].Value.ToString();
                lb_Serial_inputLS.Text = radGridView7.Rows[row].Cells["Serial_inputLS"].Value.ToString();
                lb_index.Text = row.ToString();
                lb_wait_inputLS.Text = radGridView7.Rows[row].Cells["Wait_input"].Value.ToString();
            }
            if (radGridView7.Rows[row].Cells["Wait_input"].Value.ToString() == "2")
                MessageBox.Show("Chi tiết này thuộc nhóm chuyển, liên hệ quản trị viên nếu muốn sửa");

        }
        private void search_inputLS()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = "prc_Search_LSinput";
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                lb_Serial_inputLS.Text = "0";
                lb_Serial_LS.Text = "0";
                Serial1 = txt_Serial_inputLS.Text;
                cmd.Parameters.AddWithValue("@Serial", Serial1);
                if (radGridView7.Rows.Count > 0)
                    radGridView7.Rows.Clear();
                DataSet ds = new DataSet();
               
                ds = prs.dts_sql(cmd, Conn, ds);
                prs.reset_page_txt(Page_NhapGo);
               
                if (ds.Tables[0].Rows.Count > 0)
                {
                    cb_NoiGiao_inputLS.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                    cb_NoiNhan_inputLS.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                    cb_AdorKD_inputLS.Text = ds.Tables[0].Rows[0]["ADorKD"].ToString();
                    cb_NguonGo_inputLS.Text = ds.Tables[0].Rows[0]["LoaiGo"].ToString();
                    cb_LyDo_inputLS.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                    txt_BienBan_inputLS.Text = ds.Tables[0].Rows[0]["BienBan"].ToString();
                    txt_ChungTu_inputLS.Text = ds.Tables[0].Rows[0]["ChungTu"].ToString();
                    //txt_SoXe_inputLS.Text = ds.Tables[0].Rows[0]["SoXe"].ToString();
                    dtp_Datebegin_inputLS.Text = ds.Tables[0].Rows[0]["NgayBegin"].ToString();
                    dtp_Dateend_inputLS.Text = ds.Tables[0].Rows[0]["NgayEnd"].ToString();
                    main_info_DisableinputLS();
                    temp_double = 0;
                    SoKhoi_d = 0;
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        radGridView7.Rows.Add((i+1).ToString(),ds.Tables[0].Rows[i]["DVD1"].ToString(), ds.Tables[0].Rows[i]["DVD2"].ToString(), ds.Tables[0].Rows[i]["DVD3"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString(), ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString(), ds.Tables[0].Rows[i]["PhanLoai"].ToString(), ds.Tables[0].Rows[i]["DVT"].ToString(), ds.Tables[0].Rows[i]["KhachHang"].ToString(), ds.Tables[0].Rows[i]["GhiChu"].ToString(), ds.Tables[0].Rows[i]["Wait"].ToString(), ds.Tables[0].Rows[i]["Nhom"].ToString(), ds.Tables[0].Rows[i]["ID"].ToString(),ds.Tables[0].Rows[i]["Serial_LS"].ToString());
                        temp_double += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                        SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString());
                    }
                    if (SoKhoi_d == 0)
                        lb_TongKhoi_inputLS.Text = "0";
                    else
                        lb_TongKhoi_inputLS.Text = string.Format("{0:###,###.##}", SoKhoi_d);
                    if (temp_double == 0)
                        lb_TongSL_inputLS.Text = "0";
                    else
                        lb_TongSL_inputLS.Text = string.Format("{0:###,###}", temp_double);
                }
                else
                    MessageBox.Show("Không có dữ liệu vui lòng kiểm tra lại thông tin tìm kiếm");
                ds.Clear();
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("Kiểm tra lại Serial " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_Search_inputLS_Click(object sender, EventArgs e)
        {
            search_inputLS();
            bt_save_inputLS.Enabled = false;
            bt_edit_inputLS.Enabled = true;
            bt_delete_inputLS.Enabled = true;
        }

        private void radGridView7_Click(object sender, EventArgs e)
        {
            if (radGridView7.Rows.Count > 0)
            {
                int r = radGridView7.CurrentRow.Index;
                Display_inputLS(r);
            }
        }

        private void bt_Prev_inputLS_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView7.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_inputLS(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void bt_next_inputLS_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView7.Rows.Count > 0)
                {

                    i = int.Parse(lb_index.Text);
                    if (i < radGridView7.Rows.Count - 1)
                    {
                        i++;
                        Display_inputLS(i);
                        lb_index.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void edit_inputLS()
        {
            MessageBox.Show("Phần mềm chưa hỗ trợ chức năng này. Bạn có thể xóa đi, sau đó nhập lại");
            /*Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            DVT1 = cb_DVT_inputLS.Text.Trim();
            KhachHang1 = cb_KhachHang_inputLS.Text.Trim();
            GhiChu1 = txt_GhiChu_inputLS.Text;
            count = 0;
            cmd.CommandType = CommandType.StoredProcedure;
            Serial1 = lb_Serial_inputLS.Text;
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (TL == DialogResult.Yes)
            {
                try
                {
                    if (rd_Phach_inputLS.IsChecked == true)
                    {
                        for (i = 0; i < cb_QuyCach_inputLS.Items.Count; i++)
                        {
                            if (cb_QuyCach_inputLS.Text == cb_QuyCach_inputLS.Items[i].ToString())
                            {
                                count++;
                                break;
                            }
                        }
                        if (count == 0)
                            MessageBox.Show("Nhập sai quy cách");
                        else
                        {
                            count = 0;
                            string_temp = prs.substring_right(cb_QuyCach_inputLS.Text, child);
                            string[] temp = string_temp.Split('x');
                            DVD1 = temp[0].ToString().Trim();
                            DVD2 = temp[1].ToString().Trim();
                            DVD3 = temp[2].ToString().Trim();
                            temp.Clone();
                            PhanLoai1 = cb_PhanLoai_inputLS.Text.Trim();
                            SoLuong1 = double.Parse(txt_SLPhach_inputLS.Text.Trim()).ToString();
                            //temp_double = double.Parse(DVD1) * double.Parse(DVD2) * double.Parse(DVD3) * double.Parse(SoLuong1) / 1000000000;
                            //SoKhoi1 = temp_double.ToString();
                            cmd.CommandText = "UpdNhapPhachLS";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@CRong", DVD2);
                            cmd.Parameters.AddWithValue("@CDai", DVD3);
                            cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            count = 1;
                        }


                    }
                    if (rd_Van_inputLS.IsChecked == true)
                    {

                        DVD1 = txt_CDay_inputLS.Text.Trim();
                        SoLuong1 = double.Parse(txt_SLVan_inputLS.Text.Trim()).ToString();
                        SoKhoi1 = txt_SoKhoiVan_inputLS.Text.Trim();
                        if (DVD1 == "" || SoLuong1 == "" || SoKhoi1 == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            cmd.CommandText = "UpdNhapVanLS";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDay", DVD1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@SoKhoiNhap", SoKhoi1);
                            cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            count = 1;

                        }

                    }
                    if (rd_GoTron_inputLS.IsChecked == true)
                    {
                        DVD2 = double.Parse(txt_CDaiGT_inputLS.Text.Trim()).ToString();
                        DVD1 = double.Parse(txt_VanhGT_inputLS.Text.Trim()).ToString();
                        SoLuong1 = double.Parse(txt_SLGT_inputLS.Text.Trim()).ToString();

                        if (DVD3 == "" || DVD2 == "" || SoLuong1 == "")
                            MessageBox.Show("Nhập thiếu thông tin");
                        else
                        {
                            cmd.CommandText = "UpdNhapGTLS";
                            cmd.Parameters.AddWithValue("@Serial", Serial1);
                            cmd.Parameters.AddWithValue("@CDai", DVD2);
                            cmd.Parameters.AddWithValue("@Vanh", DVD1);
                            //cmd.Parameters.AddWithValue("@DVT", DVD3);
                            //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.ExecuteNonQuery();
                            cmd.Parameters.Clear();
                            //temp_double = double.Parse(DVD2) * double.Parse(DVD2) * double.Parse(DVD1) / (4 * 3.1415926 * 1000000000);
                            //SoKhoi1 = temp_double.ToString();
                            count = 1;
                        }
                    }
                    if (count == 1)
                    {
                        MessageBox.Show("Chỉnh sửa thành công");
                        bt_rework_inputLS_Click(null, null);
                    }

                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }*/
        }
        private void bt_edit_inputLS_Click(object sender, EventArgs e)
        {
            edit_inputLS();
        }
        private void delete_inputLS()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                if (radGridView7.Rows.Count > 0)
                {
                    
                    DialogResult TL;
                    TL = MessageBox.Show("BẠN CÓ MUỐN XÓA ID: " + lb_Serial_inputLS.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (TL == DialogResult.Yes)
                    {
                        
                        if (lb_wait_inputLS.Text == "1")
                        {
                            if (rd_Phach_inputLS.IsChecked == true)
                            {
                                cmd.CommandText = "Trans_DelLSPhachKG";
                                cmd.Parameters.AddWithValue("@Serial_LS", lb_Serial_LS.Text);
                                cmd.ExecuteNonQuery();
                                //string_temp = @"DELETE FROM [dbo].[Nhap_PhachLS] WHERE Serial=" + lb_Serial_inputLS.Text;
                            }
                            if (rd_Van_inputLS.IsChecked == true)
                            {
                                cmd.CommandText = "Trans_DelLSVanKG";
                                cmd.Parameters.AddWithValue("@Serial_LS", lb_Serial_LS.Text);
                                cmd.ExecuteNonQuery();
                            }
                            if (rd_GoTron_inputLS.IsChecked == true)
                            {
                                cmd.CommandText = "Trans_DelLSGTKG";
                                cmd.Parameters.AddWithValue("@Serial_LS", lb_Serial_LS.Text);
                                cmd.ExecuteNonQuery();
                            }
                           
                            MessageBox.Show("Dữ liệu được xóa thành công");
                            bt_rework_inputLS_Click(null, null);
                        }
                        else
                            MessageBox.Show("Mã kiện này đã giao Kho Gỗ hoặc giao Sản Xuất rồi, bạn không được phép xóa");
                    }
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("ĐÃ XẢY RA LỖI. DỮ LIỆU XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }

        private void bt_delete_inputLS_Click(object sender, EventArgs e)
        {
            delete_inputLS();
        }

        private void print_inputLS()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Datebegin_inputLS.Text);
                if (rd_Phach_inputLS.IsChecked == true)
                {
                    string_temp = @" select dbo.ChungTuLoSay.Serial,ChungTuLoSay.NoiGiao,ChungTuLoSay.NoiNhan,ChungTuLoSay.ADorKD,ChungTuLoSay.BienBan as SoBB,ChungTuLoSay.ChungTu,ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.Serial,convert(varchar(10),ChungTuLoSay.NgayBegin,103) as Ngay
                                ,dbo.Nhap_PhachLS.CDai,Nhap_PhachLS.CDay,Nhap_PhachLS.CRong,Nhap_PhachLS.KhachHang,Nhap_PhachLS.SLNhap,(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,Nhap_PhachLS.SLXuat,(CDay*CRong*CDai*SLXuat)/1000000000 as SoKhoiXuat,Nhap_PhachLS.PhanLoai,Nhap_PhachLS.GhiChu
                                from dbo.Nhap_PhachLS inner join dbo.ChungTuLoSay on (dbo.Nhap_PhachLS.Serial_ChungTu=ChungTuLoSay.Serial";
                    if (cb_QuyCach_inputLS.Text != "")
                    {
                        string Ngay_temp = prs.substring_right(cb_QuyCach_inputLS.Text, child);
                        string[] temp = Ngay_temp.Split('x');
                        DVD1 = temp[0].ToString().Trim();
                        DVD2 = temp[1].ToString().Trim();
                        DVD3 = temp[2].ToString().Trim();
                        temp.Clone();

                        GhiChu1 += " and Nhap_PhachLS.CDay=" + DVD1 + " and Nhap_PhachLS.CRong=" + DVD2 + " and Nhap_PhachLS.CDai=" + DVD3;
                    }
                    if (cb_PhanLoai_inputLS.Text != "")
                        GhiChu1 += "and Nhap_PhachLS.PhanLoai='" + cb_PhanLoai_inputLS.Text + "'";
                }
                if (rd_Van_inputLS.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuLoSay.Serial,ChungTuLoSay.NoiGiao,ChungTuLoSay.NoiNhan,ChungTuLoSay.ADorKD,ChungTuLoSay.BienBan as SoBB,ChungTuLoSay.ChungTu,ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.Serial,convert(varchar(10),ChungTuLoSay.NgayBegin,103) as Ngay
                                ,Nhap_VanLS.CDay,Nhap_VanLS.KhachHang,Nhap_VanLS.SLNhap,SoKhoiNhap,Nhap_VanLS.SLXuat,SoKhoiXuat,Nhap_VanLS.GhiChu
                                from dbo.Nhap_VanLS inner join dbo.ChungTuLoSay on (dbo.Nhap_VanLS.Serial_ChungTu=ChungTuLoSay.Serial";
                    if (txt_CDay_inputLS.Text != "")
                        GhiChu1 += " and Nhap_VanLS.CDay=" + txt_CDay_inputLS.Text;
                }
                if (rd_GoTron_inputLS.IsChecked == true)
                {
                    string_temp = @"select dbo.ChungTuLoSay.Serial,ChungTuLoSay.NoiGiao,ChungTuLoSay.NoiNhan,ChungTuLoSay.ADorKD,ChungTuLoSay.BienBan as SoBB,ChungTuLoSay.ChungTu,ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.Serial,convert(varchar(10),ChungTuLoSay.NgayBegin,103) as Ngay
                                ,dbo.Nhap_GoTronLS.CDai,Nhap_GoTronLS.Vanh,Nhap_GoTronLS.KhachHang,Nhap_GoTronLS.SLNhap,(Vanh*Vanh*CDai*SLNhap)/(4*3.141593*1000000000) as SoKhoiNhap,Nhap_GoTronLS.SLXuat,(Vanh*Vanh*CDai*SLXuat)/(4*3.141593*1000000000) as SoKhoiXuat,ChungTuLoSay.GhiChu
                                from dbo.Nhap_GoTronLS inner join dbo.ChungTuLoSay on (dbo.Nhap_GoTronLS.Serial_ChungTu=ChungTuLoSay.Serial";
                    if (txt_VanhGT_inputLS.Text != "")
                        GhiChu1 += " and Nhap_GoTronLS.Vanh=" + txt_VanhGT_inputLS.Text;
                    if (txt_CDaiGT_inputLS.Text != "")
                        GhiChu1 += " and Nhap_GoTronLS.CDai=" + txt_CDaiGT_inputLS.Text;
                }
                if (cb_NoiGiao_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.NoiGiao='" + prs.substring_left(cb_NoiGiao_inputLS.Text, child) + "'";
                if (cb_NoiNhan_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.NoiNhan='" + prs.substring_left(cb_NoiNhan_inputLS.Text, child) + "'";
                if (cb_NguonGo_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.LoaiGo='" + cb_NguonGo_inputLS.Text + "'";
                if (cb_LyDo_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.LyDo='" + prs.substring_right(cb_LyDo_inputLS.Text, child) + "'";
                if (txt_ChungTu_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.ChungTu='" + txt_ChungTu_inputLS.Text + "'";
                if (txt_BienBan_inputLS.Text != "")
                    GhiChu1 += " and ChungTuLoSay.BienBan='" + txt_BienBan_inputLS.Text + "'";
                if (cb_AdorKD_inputLS.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_inputLS.Text + "'";
                string_temp += GhiChu1 + " and SLNhap<>0 and ChungTuLoSay.NgayBegin='" + Ngay1 + "')";
                //MessageBox.Show(string_temp);
                MessageBox.Show(GhiChu1);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Datebegin_inputLS.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);

                if (rd_Phach_inputLS.IsChecked == true || rd_Van_inputLS.IsChecked == true)
                {
                    FrR.CrR_LS_NKPhachSC(ds, Ngay1);
                }
                if (rd_GoTron_inputLS.IsChecked == true)
                    FrR.CrR_LS_NKGoTron(ds, Ngay1);

                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }
        private void txt_QC3_inputLS_Leave(object sender, EventArgs e)
        {
            try
            {

                if (txt_QC3_inputLS.Text != "")
                {
                    Conn = prs.Connect();
                    Conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = Conn;
                    string_temp = txt_QC1_inputLS.Text.Trim() + txt_QC2_inputLS.Text.Trim() + txt_QC3_inputLS.Text.Trim();
                    if (txt_QC3_inputLS.Text != "")
                    {
                        string_temp = txt_QC1_inputLS.Text.Trim() + txt_QC2_inputLS.Text.Trim() + txt_QC3_inputLS.Text.Trim();

                        for (i = 0; i < dataGridView_QuyCach.Rows.Count - 1; i++)
                        {
                            //MessageBox.Show(string_temp);
                            if (string_temp == dataGridView_QuyCach.Rows[i].Cells["QuyCach"].Value.ToString())
                            {

                                cb_PhanLoai_inputLS.Text = dataGridView_QuyCach.Rows[i].Cells["PhanLoai"].Value.ToString();
                                cb_QuyCach_inputLS.Text = dataGridView_QuyCach.Rows[i].Cells["QuyCach"].Value.ToString() + " - " + dataGridView_QuyCach.Rows[i].Cells["size"].Value.ToString();
                                //MessageBox.Show(cb_QuyCach_inputLS.Text, "aaa");
                                break;
                            }
                        }




                        // MessageBox.Show(i.ToString());

                        if (i == dataGridView_QuyCach.Rows.Count - 1)
                        {
                            MessageBox.Show("Bạn đang nhập sai quy cách");
                            cb_QuyCach_inputLS.Text = "";
                        }
                       
                        else
                        {
                            DVD1 = txt_QC1_inputLS.Text.Trim();
                            DVD2 = txt_QC2_inputLS.Text.Trim();
                            DVD3 = txt_QC3_inputLS.Text.Trim();
                            //temp.Clone();
                            cmd.CommandText = "select KhoGo_TK.SoKhoiTon from dbo.KhoGo_TK where DVD1=" + DVD1 + " and DVD2=" + DVD2 + " and DVD3=" + DVD3 + " and ADorKD='" + cb_AdorKD_inputLS.Text + "'";
                            //MessageBox.Show(cmd.CommandText.ToString());
                            lb_TonKhoKG.Text = cmd.ExecuteScalar().ToString();
                        }
                    }
                    Conn.Close();
                    cmd.Dispose();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            /*try
            {
                if (txt_QC3_inputLS.Text != "")
                {
                    string_temp = txt_QC1_inputLS.Text.Trim() + txt_QC2_inputLS.Text.Trim() + txt_QC3_inputLS.Text.Trim();
                    for (i = 0; i < cb_QuyCach_inputLS.Items.Count; i++)
                    {

                        if (string_temp == prs.substring_left(cb_QuyCach_inputLS.Items[i].Text, child))
                        {
                            cb_QuyCach_inputLS.Text = cb_QuyCach_inputLS.Items[i].Text;
                            if (double.Parse(txt_QC3_inputLS.Text) % 100 == 0 && double.Parse(txt_QC3_inputLS.Text) > 1000)
                            {
                                cb_PhanLoai_inputLS.Text = "Phach";

                            }
                            else
                                cb_PhanLoai_inputLS.Text = "So Che";
                            break;
                        }
                    }
                    if (i == cb_QuyCach_inputLS.Items.Count)
                    {
                        MessageBox.Show("Bạn đang nhập sai quy cách");
                        cb_QuyCach_inputLS.Text = "";
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }*/

        }
        private void print_TH_inputLS()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Datebegin_inputLS.Text);

                string_temp = @" SELECT [NoiGiao],[NoiNhan],[ADorKD],[LoaiGo] ,[LyDo] ,[BienBan] as SoBB,[ChungTu],[DVD1],[DVD2],[DVD3],[PhanLoai],[SLNhap] ,[SoKhoiNhap]
                                     ,[KhachHang],[GhiChu],[Serial_ChungTu] as Serial
                                    FROM [dbo].[NhapGoLS] where NgayBegin='"+Ngay1+"' ";

                if (cb_NoiGiao_inputLS.Text != "")
                    GhiChu1 += " and NoiGiao='" + prs.substring_left(cb_NoiGiao_inputLS.Text, child) + "'";
                if (cb_NoiNhan_inputLS.Text != "")
                    GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_inputLS.Text, child) + "'";
                if (cb_NguonGo_inputLS.Text != "")
                    GhiChu1 += " and LoaiGo='" + cb_NguonGo_inputLS.Text + "'";
                if (cb_LyDo_inputLS.Text != "")
                    GhiChu1 += " and LyDo='" + prs.substring_right(cb_LyDo_inputLS.Text, child) + "'";
                if (txt_ChungTu_inputLS.Text != "")
                    GhiChu1 += " and ChungTu='" + txt_ChungTu_inputLS.Text + "'";
                if (txt_BienBan_inputLS.Text != "")
                    GhiChu1 += " and BienBan='" + txt_BienBan_inputLS.Text + "'";
                if (cb_AdorKD_inputLS.Text != "")
                    GhiChu1 += " and ADorKD ='" + cb_AdorKD_inputLS.Text + "'";
               // string_temp += GhiChu1 + " and NgayBegin='" + Ngay1 + "'";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày: " + dtp_Datebegin_inputLS.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);


                FrR.CrR_LS_NhapKho(ds, Ngay1);


                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }




        #endregion


        #region //Chung Tu Lo Say
        private void search_CTLS()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            cmd.CommandText = "prc_Search_LSinput";
            cmd.CommandType = CommandType.StoredProcedure;
            try
            {

                Serial1 = txt_SerialCT_CTLS.Text;
                cmd.Parameters.AddWithValue("@Serial", Serial1);
                if (radGridView8.Rows.Count > 0)
                    radGridView8.Rows.Clear();
                DataSet ds = new DataSet();
                ds = prs.dts_sql(cmd, Conn, ds);
                //prs.reset_page_txt(Page_ChungTu);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    cb_NoiGiao_CTLS.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                    cb_NoiNhan_CTLS.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                    cb_AdorKD_CTLS.Text = ds.Tables[0].Rows[0]["ADorKD"].ToString();
                    cb_LoaiGo_CTLS.Text = ds.Tables[0].Rows[0]["LoaiGo"].ToString();
                    cb_LyDo_CTLS.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                    txt_BienBan_CTLS.Text = ds.Tables[0].Rows[0]["BienBan"].ToString();
                    txt_ChungTu_CTLS.Text = ds.Tables[0].Rows[0]["ChungTu"].ToString();
                    //txt_SoXe_CTLS.Text = ds.Tables[0].Rows[0]["SoXe"].ToString();
                    dtp_DateBegin_CTLS.Text = ds.Tables[0].Rows[0]["NgayBegin"].ToString();
                    dtp_DateEnd_CTLS.Text = ds.Tables[0].Rows[0]["NgayEnd"].ToString();

                    temp_double = 0;
                    SoKhoi_d = 0;

                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        radGridView8.Rows.Add(ds.Tables[0].Rows[i]["DVD1"].ToString(), ds.Tables[0].Rows[i]["DVD2"].ToString(), ds.Tables[0].Rows[i]["DVD3"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString(), ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString(), ds.Tables[0].Rows[i]["Wait"].ToString());
                        temp_double += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                        //SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiXuat"].ToString());
                    }
                    lb_SoKhoi_CTLS.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                    lb_SoLuong_CTLS.Text = string.Format("{0:###,###}", temp_double);
                    string_temp = ds.Tables[0].Rows[0]["Nhom"].ToString();
                    if (string_temp == "Phach")
                    {
                        radGridView8.Columns["DVD1_CTLS"].HeaderText = "C Dày";
                        radGridView8.Columns["DVD2_CTLS"].HeaderText = "C Rộng";
                        radGridView8.Columns["DVD3_CTLS"].HeaderText = "C Dài";
                    }
                    if (string_temp == "Van")
                    {
                        radGridView8.Columns["DVD1_CTLS"].HeaderText = "C Dày";
                        radGridView8.Columns["DVD2_CTLS"].HeaderText = "";
                        radGridView8.Columns["DVD3_CTLS"].HeaderText = "";
                    }
                    if (string_temp == "GoTron")
                    {
                        radGridView8.Columns["DVD2_CTLS"].HeaderText = "C Dài";
                        radGridView8.Columns["DVD1_CTLS"].HeaderText = "Vanh";
                        radGridView8.Columns["DVD3_CTLS"].HeaderText = "";
                    }
                }
                else
                    MessageBox.Show("Không có dữ liệu vui lòng kiểm tra lại thông tin tìm kiếm");
                ds.Clear();
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("Kiểm tra lại Serial " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_Search_CTLS_Click(object sender, EventArgs e)
        {
            search_CTLS();
        }
        private void edit_searchCTLS()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;

            Serial1 = txt_SerialCT_CTLS.Text;
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SERIAL " + Serial1 + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (TL == DialogResult.Yes)
            {
                try
                {
                    if (cb_NoiNhan_CTLS.Text != "" && cb_NoiNhan_CTLS.Text != "" && cb_LyDo_CTLS.Text != "" && cb_AdorKD_CTLS.Text != "" && cb_LoaiGo_CTLS.Text != "" || txt_SerialCT_CTLS.Text != "")
                    {
                        NoiGiao1 = prs.substring_left(cb_NoiGiao_CTLS.Text, child);
                        if (NoiGiao1 == "")
                            NoiGiao1 = cb_NoiGiao_CTLS.Text;
                        NoiNhan1 = prs.substring_left(cb_NoiNhan_CTLS.Text, child);
                        if (NoiNhan1 == "")
                            NoiNhan1 = cb_NoiNhan_CTLS.Text;
                        LyDo1 = prs.substring_right(cb_LyDo_CTLS.Text, child);
                        if (LyDo1 == "")
                            LyDo1 = cb_LyDo_CTLS.Text;
                        count = 0;
                        for (i = 0; i < cb_NoiNhan_CTLS.Items.Count; i++)
                        {
                            if (NoiGiao1 == prs.substring_left(cb_NoiNhan_CTLS.Items[i].ToString(), child))
                                count++;
                            if (NoiNhan1 == prs.substring_left(cb_NoiNhan_CTLS.Items[i].ToString(), child))
                                count++;
                            if (count == 2)
                                break;

                        }
                        if (count == 2)
                        {

                            for (i = 0; i < cb_LyDo_CTLS.Items.Count; i++)
                            {
                                if (LyDo1 == prs.substring_right(cb_LyDo_CTLS.Items[i].ToString(), child))
                                {
                                    count++;
                                    break;
                                }
                            }
                            if (count == 3)
                            {


                                ADorKD1 = cb_AdorKD_CTLS.Text.Trim();
                                LoaiGo1 = cb_LoaiGo_CTLS.Text.Trim();

                                BienBan1 = txt_BienBan_CTLS.Text.Trim();
                                ChungTu1 = txt_ChungTu_CTLS.Text.Trim();
                                //SoXe1 = txt_SoXe_CTLS.Text.Trim();
                                Datebegin = Date_Split(dtp_DateBegin_CTLS.Text);
                                Dateend = Date_Split(dtp_DateEnd_CTLS.Text);
                                cmd.CommandText = @"UPDATE [ChungTuLoSay] SET [NoiGiao] ='" + NoiGiao1 + "' ,[NoiNhan] ='" + NoiNhan1 + "' ,[ADorKD] = '" + ADorKD1 + "',[LoaiGo] ='" + LoaiGo1 + "' ,[LyDo] ='" + LyDo1 + "' ,[BienBan] ='" + BienBan1 + "' ,[ChungTu] ='" + ChungTu1 + "',[NgayBegin] ='" + Datebegin + "',NgayEnd='"+Dateend+"' WHERE Serial= " + Serial1;
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Dữ liệu được sửa thành công");
                                radGridView8.Rows.Clear();
                                txt_SerialCT_CTLS.Text = "";
                            }
                            else
                                MessageBox.Show("Kiểm tra lại Lý do");
                        }
                        else
                            MessageBox.Show("Kiểm tra lại Nơi giao hoặc Nơi nhận");
                    }
                    else
                        MessageBox.Show("Nhập thiếu thông tin");

                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void bt_edit_CTLS_Click(object sender, EventArgs e)
        {
            edit_searchCTLS();
        }

        private void bt_delete_CTLS_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;

            Serial1 = txt_SerialCT_CTLS.Text;
            int_temp = radGridView8.Rows.Count;
            if (int_temp > 0)
            {
                DialogResult TL;
                MessageBox.Show("TẤT CẢ CÁC LÔ HÀNG TRONG DANH SÁCH PHÍA DƯỚI SẼ BỊ XÓA??");
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA SERIAL " + Serial1 + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (TL == DialogResult.Yes)
                {
                    try
                    {
                        for(i=0;i<int_temp;i++)
                        {
                            if (radGridView8.Rows[i].Cells["Wait_CTLS"].Value.ToString() == "0")
                                break;
                        }
                        if (i == int_temp)
                        {
                            cmd.CommandText = @"DELETE FROM [ChungTuLoSay] WHERE Serial=" + Serial1;
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Dữ liệu được xóa thành công");
                            radGridView8.Rows.Clear();
                        }
                        else
                        {
                            MessageBox.Show("Chứng từ này đã có ít nhất một kiện được giao sản xuất, nên không được phép xóa");
                        }

                    }
                    catch (Exception ex)
                    {
                        DialogResult Err;
                        Err = MessageBox.Show("ĐÃ XẢY RA LỖI, XÓA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        Conn.Close();
                        cmd.Dispose();
                    }
                }
            }
            else
                MessageBox.Show("Bạn cần load thông tin của chứng từ này lên để kiểm tra trước nhấn: <<Tìm>>");
        }
        private void bt_final_CTLS_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;

            Serial1 = txt_SerialCT_CTLS.Text;
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN XÁC NHẬN CHO SERIAL " + Serial1 + " RA LÒ KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (TL == DialogResult.Yes)
            {
                try
                {
                    
                                cmd.CommandText = @"UPDATE [ChungTuLoSay] SET Wait=N'Da ra lo' WHERE Serial= " + Serial1;
                                cmd.ExecuteNonQuery();
                                MessageBox.Show("Chứng từ được xác nhận đã được ra lò");
                                radGridView8.Rows.Clear();
                               
                   
                }
                catch (Exception ex)
                {
                    DialogResult Err;
                    Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        #endregion
        #region // Bao Cao LS
        private DataSet Report_LS()
        {

            DataSet ds = new DataSet();
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DVT1 = "";

            /*if (rd_Phach_BCLS.IsChecked == true)
            {
                GhiChu1 = @"select ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.NoiNhan,ChungTuLoSay.NoiGiao
                            ,Nhap_PhachLS.CDay,Nhap_PhachLS.CRong,Nhap_PhachLS.CDai,SLNhap,(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,convert(varchar(10),ChungTuLoSay.NgayBegin,103) as NgayBegin,convert(varchar(10),ChungTuLoSay.NgayEnd,103) as NgayEnd
                            from dbo.Nhap_PhachLS inner join ChungTuLoSay on (ChungTuLoSay.Serial=Nhap_PhachLS.Serial_ChungTu and Nhap_PhachLS.Wait=1) ";
                
            }
            if (rd_Van_BCLS.IsChecked == true)
            {
                GhiChu1 = @"select ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.NoiNhan,ChungTuLoSay.NoiGiao
                            ,Nhap_VanLS.CDay,SLNhap,SoKhoiNhap,convert(varchar(10),ChungTuLoSay.NgayBegin,103) as NgayBegin,convert(varchar(10),ChungTuLoSay.NgayEnd,103) as NgayEnd
                            from dbo.Nhap_VanLS inner join ChungTuLoSay on (ChungTuLoSay.Serial=Nhap_VanLS.Serial_ChungTu and Nhap_VanLS.Wait=1)
                           ";
            }
            if (rd_GoTron_BCLS.IsChecked == true)
            {
                GhiChu1 = @"select ChungTuLoSay.LoaiGo,ChungTuLoSay.LyDo,ChungTuLoSay.NoiNhan,ChungTuLoSay.NoiGiao
                            ,Nhap_GoTronLS.CDai,Nhap_GoTronLS.Vanh,SLNhap, (Vanh * Vanh * CDai * SLNhap)
                                             / (4 * 3.1415926 * 1000000000) AS SoKhoiNhap,convert(varchar(10),ChungTuLoSay.NgayBegin,103) as NgayBegin,convert(varchar(10),ChungTuLoSay.NgayEnd,103) as NgayEnd
                            from dbo.Nhap_GoTronLS inner join ChungTuLoSay on (ChungTuLoSay.Serial=Nhap_GoTronLS.Serial_ChungTu and Nhap_GoTronLS.Wait=1)
                           ";
            }*/
            GhiChu1 = @"SELECT [NoiGiao],[NoiNhan],[ADorKD],[LoaiGo],[LyDo],convert(varchar(10),[NgayBegin],103) as NgayBegin,convert(varchar(10),[NgayEnd],103) as NgayEnd,[DVD1] as CDay,[DVD2] as CRong ,[DVD3] as CDai,[PhanLoai],[SLNhap],[SoKhoiNhap]
    
                          FROM [dbo].[NhapGoLS]
                          where Wait='1'";
            ds = prs.dts(GhiChu1, Conn, ds);
            Conn.Close();
            return ds;
            /*string DateBeginVL, DateBeginRL, DateEndVL, DateEndRL;
            if (dtp_DateBeginVL_BCLS.Text != "")
            {
                DateBeginVL = Date_Split(dtp_DateBeginVL_BCLS.Text) + " 00:00";
            }
            if (dtp_DateEndVL_BCLS.Text != "")
            {
                DateEndVL = Date_Split(dtp_DateEndVL_BCLS.Text) + " 23:59";
            }
            if (dtp_DateBeginRL_BCLS.Text != "")
            {
                DateBeginRL = Date_Split(dtp_DateBeginRL_BCLS.Text) + " 00:00";
            }
            if (dtp_DateEndRL_BCLS.Text != "")
            {
                DateEndRL = Date_Split(dtp_DateEndRL_BCLS.Text) + " 23:59";
            }

            //begin + " 00:00";
         
            
            if (cb_NoiGiao_BCKG.Text != "")
            {
                GhiChu1 += " and NoiGiao='" + prs.substring_left(cb_NoiGiao_BCKG.Text, child) + "'";
                DVT1 += " Nơi giao: " + prs.substring_right(cb_NoiGiao_BCKG.Text, child) + "\n";
            }
            if (cb_NoiNhan_BCKG.Text != "")
            {
                GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_BCKG.Text, child) + "'";
                DVT1 += " Nơi nhận: " + prs.substring_right(cb_NoiNhan_BCKG.Text, child) + "\n";
            }
            if (cb_AdorKD_BCKG.Text != "")
            {
                GhiChu1 += " and ADorKD='" + cb_AdorKD_BCKG.Text + "'";
                DVT1 += " ADorKD: " + cb_AdorKD_BCKG.Text + "     ";
            }
            if (cb_LoaiGo_BCKG.Text != "")
            {
                GhiChu1 += " and LoaiGo='" + cb_LoaiGo_BCKG.Text + "'";
                DVT1 += " Loại gỗ: " + cb_LoaiGo_BCKG.Text + "      ";
            }
            if (cb_LyDo_BCKG.Text != "")
            {
                GhiChu1 += " and LyDo='" + prs.substring_right(cb_LyDo_BCKG.Text, child) + "'";
                DVT1 += " Lý Do: " + prs.substring_left(cb_LyDo_BCKG.Text, child);
            }
            if (rd_Phach_BCKG.IsChecked == true)
            {
                string_temp = @"select CDay,CRong,CDai,PhanLoai as GhiChu,SUM(qry1.SLTonDau) as SLTonDau,SUM(qry1.SKTonDau) as SKTonDau,SUM(SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,SUM(SLTonDau)+SUM(SLNhap)-SUM(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon  from
                                    (SELECT [CDay],[CRong],[CDai],[PhanLoai],SLTonDau=0,SKTonDau=0,sum([SLNhap]) as SLNhap,sum([SLXuat]) as SLXuat,sum(CDay*CRong*CDai*SLNhap)/1000000000 as SoKhoiNhap,sum(CDay*CRong*CDai*SLXuat)/1000000000 as SoKhoiXuat   
                                    FROM [dbo].[Nhap_PhachLS] inner join dbo.ChungTuLoSay on ";
                string_temp += "                (ChungTuLoSay.Serial=Nhap_PhachLS.Serial_ChungTu";

                string_temp += GhiChu1;
                string_temp += " and ChungTuLoSay.Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "') group by [CDay],[CRong],[CDai],[PhanLoai]";
                string_temp += @" union all 
                                    SELECT [CDay],[CRong],[CDai],[PhanLoai],SUM(SLNhap)-SUM(SLXuat) as SLTonDau,(sum(CDay*CRong*CDai*SLNhap)-sum(CDay*CRong*CDai*SLXuat))/1000000000 as SKTonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0
                                    from Nhap_PhachLS inner join dbo.ChungTuLoSay on 
                                    (ChungTuLoSay.Serial=Nhap_PhachLS.Serial_ChungTu ";
                string_temp += GhiChu1;
                string_temp += @" and ChungTuLoSay.Ngay<'" + Datebegin + "') group by [CDay],[CRong],[CDai],[PhanLoai]) as qry1 group by CDay,CRong,CDai,PhanLoai";

            }
            if (rd_Van_BCKG.IsChecked == true)
            {
                string_temp = @"select CDay as DVD1,SUM(qry1.SLTonDau) as SLTonDau,SUM(qry1.SKTonDau) as SKTonDau,SUM(SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,SUM(SLTonDau)+SUM(SLNhap)-SUM(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon  from
                                    (SELECT [CDay],SLTonDau=0,SKTonDau=0,sum([SLNhap]) as SLNhap,sum([SLXuat]) as SLXuat,sum(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat   
                                    FROM [dbo].[Nhap_VanLS] inner join dbo.ChungTuLoSay on 
                                    (ChungTuLoSay.Serial=Nhap_VanLS.Serial_ChungTu ";
                string_temp += GhiChu1;
                string_temp += @"                 and ChungTuLoSay.Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "') group by [CDay]";
                string_temp += @"                 union all 
                                    SELECT [CDay],SUM(SLNhap)-SUM(SLXuat) as SLTonDau,sum(SoKhoiNhap)-sum(SoKhoiXuat) as SKTonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0
                                    from Nhap_VanLS inner join dbo.ChungTuLoSay on 
                                    (ChungTuLoSay.Serial=Nhap_VanLS.Serial_ChungTu ";
                string_temp += GhiChu1;
                string_temp += @" and ChungTuLoSay.Ngay<'" + Datebegin + "') group by [CDay])  as qry1 group by CDay";
            }
            if (rd_GoTron_BCKG.IsChecked == true)
            {
                string_temp = @"select Vanh,CDai,SUM(qry1.SLTonDau) as SLTonDau,SUM(qry1.SKTonDau) as SKTonDau,SUM(SLNhap) as SLNhap,SUM(SLXuat) as SLXuat,SUM(SoKhoiNhap) as SoKhoiNhap,sum(SoKhoiXuat) as SoKhoiXuat,SUM(SLTonDau)+SUM(SLNhap)-SUM(SLXuat) as SLTon,SUM(SKTonDau)+SUM(SoKhoiNhap)-SUM(SoKhoiXuat) as SoKhoiTon  from
                                    (SELECT [CDai],Vanh,SLTonDau=0,SKTonDau=0,sum([SLNhap]) as SLNhap,sum([SLXuat]) as SLXuat,sum(CDai*Vanh*Vanh*SLNhap)/(4 * 3.1415926 * 1000000000) as SoKhoiNhap,sum(CDai*Vanh*Vanh*SLXuat)/(4 * 3.1415926 * 1000000000) as SoKhoiXuat   
                                    FROM [dbo].[Nhap_GoTronLS] inner join dbo.ChungTuLoSay on 
                                    (ChungTuLoSay.Serial=Nhap_GoTronLS.Serial_ChungTu ";
                string_temp += GhiChu1;
                string_temp += @"                 and ChungTuLoSay.Ngay>='" + Datebegin + "' and Ngay<='" + Dateend + "') group by [CDai],Vanh";
                string_temp += @"                 union all 
                                    SELECT [CDai],Vanh,SUM(SLNhap)-SUM(SLXuat) as SLTonDau,(sum(CDai*Vanh*Vanh*SLNhap)-sum(CDai*Vanh*Vanh*SLXuat))/(4 * 3.1415926 * 1000000000) as SKTonDau,SLNhap=0,SLXuat=0,SoKhoiNhap=0,SoKhoiXuat=0
                                    from Nhap_GoTronLS inner join dbo.ChungTuLoSay on 
                                    (ChungTuLoSay.Serial=Nhap_GoTronLS.Serial_ChungTu ";
                string_temp += @"                 and ChungTuLoSay.Ngay<'" + Datebegin + "') group by [CDai],Vanh) as qry1 group by CDai,Vanh";
            }
            //MessageBox.Show(DVT1);
            ds = prs.dts(string_temp, Conn, ds);
            Conn.Close();
            return ds;
            /**/

        }
        private void bt_Search_BCLS_Click(object sender, EventArgs e)
        {
            try
            {
                DataSet ds = Report_LS();
                Fr_Report FrR = new Fr_Report();
                //string_temp = "Từ ngày: " + dtp_Datebegin_BCKG.Text + " Đến ngày: " + dtp_Dateend_BCKG.Text;
                if (rd_Phach_BCKG.IsChecked == true || rd_Van_BCKG.IsChecked == true)
                    FrR.CrR_GoDangSay(ds.Tables[0]);
                if (rd_GoTron_BCKG.IsChecked == true)
                    FrR.CrR_GoTron_ReportKG(ds, string_temp, DVT1);
                // FrR.report_KV1NKTG(ds, Ngay1);
                FrR.Show();
                ds.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


        #endregion
        #region //Go ra lo
        private void Page_KienRaLo_Paint(object sender, PaintEventArgs e)
        {

        }
        private void Load_GoRaLo()
        {
            Conn = prs.Connect();
            Conn.Open();
            DataSet ds = new DataSet();
            try
            {
                if (radGridView9.Rows.Count > 0)
                    radGridView9.Rows.Clear();
                string_temp = @"SELECT     NoiNhan, ChungTu,ID, DVD1, DVD2, DVD3, PhanLoai, SLNhap, 
                      SoKhoiNhap, DVT,KhachHang, GhiChu,Nhom, Serial,Wait,LoaiGo
                    FROM         dbo.NhapGoLS where Wait=1";
               // MessageBox.Show(cb_NoiNhan_outputLS.Text);
                if (txt_Serial_outputLS.Text != "")
                {
                    string_temp += @" and Serial=" + txt_Serial_outputLS.Text ;
                }
                    
                else
                {
                   // MessageBox.Show(cb_NoiNhan_outputLS.Text);
                    if (cb_NoiNhan_outputLS.Text != "")
                    {
                       
                            string_temp += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_outputLS.Text, child) + "'";


                    }
                }
                string_temp += "order by GhiChu";
               // MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    cb_NguonGo_outputLS.Text=ds.Tables[0].Rows[0]["LoaiGo"].ToString();
                   
                    
                    for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        radGridView9.Rows.Add(false, ds.Tables[0].Rows[i]["DVD1"].ToString(), ds.Tables[0].Rows[i]["DVD2"].ToString(), ds.Tables[0].Rows[i]["DVD3"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString(), ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString(), ds.Tables[0].Rows[i]["PhanLoai"].ToString(), ds.Tables[0].Rows[i]["DVT"].ToString(), ds.Tables[0].Rows[i]["KhachHang"].ToString(), ds.Tables[0].Rows[i]["GhiChu"].ToString(), ds.Tables[0].Rows[i]["Wait"].ToString(), ds.Tables[0].Rows[i]["Nhom"].ToString(), ds.Tables[0].Rows[i]["ID"].ToString(),ds.Tables[0].Rows[i]["NoiNhan"].ToString());
                    }

                    lb_TongKhoi_outputLS.Text = "0";
                    lb_Kien.Text = "0";
                    bt_inputKho.Enabled = false;
                    bt_outputSX.Enabled = false;
                }
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }
        private void bt_Load_outputLS_Click(object sender, EventArgs e)
        {
            Load_GoRaLo();
        }
        private void radGridView9_ValueChanged(object sender, EventArgs e)
        {
            try
            {
                radGridView9.EndEdit();
                int r = radGridView9.CurrentCell.RowIndex;
                bool isChecked = (bool)radGridView9.CurrentRow.Cells["Check_outputLS"].Value;

                if (isChecked == true)
                {
                    radGridView9.CurrentRow.Cells["Check_outputLS"].BeginEdit();
                    //MessageBox.Show(radGridView9.CurrentRow.Cells["SoKhoi_outputLS"].Value.ToString());
                    temp_double = double.Parse(lb_TongKhoi_outputLS.Text) + double.Parse(radGridView9.CurrentRow.Cells["SoKhoi_outputLS"].Value.ToString());
                    lb_TongKhoi_outputLS.Text = temp_double.ToString();
                    count = int.Parse(lb_Kien.Text) + 1;
                    lb_Kien.Text = count.ToString();
                    radGridView9.EndEdit();
                }
                else
                {

                    //MessageBox.Show("-" + radGridView9.CurrentRow.Cells["SoKhoi_outputLS"].Value.ToString());
                    temp_double = double.Parse(lb_TongKhoi_outputLS.Text) - double.Parse(radGridView9.CurrentRow.Cells["SoKhoi_outputLS"].Value.ToString());
                    lb_TongKhoi_outputLS.Text = temp_double.ToString();
                    count = int.Parse(lb_Kien.Text) - 1;
                    lb_Kien.Text = count.ToString();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        private void NhapKho_LoSay()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Tran_Ins_CTNhap";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            if (radGridView9.Rows.Count > 0)
            {
                try
                {

                    //string insert_NhapKTam = null;
                    if (txt_ChungTu_outputLS.Text != "")
                    {
                        NoiGiao1 = "Lo Say";
                        NoiNhan1 = "CO1";
                        ADorKD1 = "KD";
                        LoaiGo1 = cb_NguonGo_outputLS.Text.Trim();
                        LyDo1 = "GNLoSay";
                        BienBan1 = txt_BienBan_outputLS.Text.Trim();
                        ChungTu1 = txt_ChungTu_outputLS.Text.Trim();

                        Ngay1 = Date_Split(dtp_Date_outputLS.Text);

                        cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                        cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                        cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                        cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                        cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                        cmd.Parameters.AddWithValue("@SoXe", "");
                        cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                        cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                        SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                        code.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(code);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                        sdr.Close();
                        cmd.Parameters.Clear();

                        //MessageBox.Show("aaaa");


                        for (i = radGridView9.Rows.Count - 1; i >= 0; i--)
                        {
                            string_temp = radGridView9.Rows[i].Cells["Nhom_outputLS"].Value.ToString();
                            //MessageBox.Show(string_temp);
                            if (string_temp == "Phach")
                            {
                                cmd.CommandText = "Trans_InsUp_NhapPhach";
                                DVD1 = radGridView9.Rows[i].Cells["DVD1_outputLS"].Value.ToString();
                                DVD2 = radGridView9.Rows[i].Cells["DVD2_outputLS"].Value.ToString();
                                DVD3 = radGridView9.Rows[i].Cells["DVD3_outputLS"].Value.ToString();
                                SoLuong1 = double.Parse(radGridView9.Rows[i].Cells["SoLuong_outputLS"].Value.ToString()).ToString();
                                //SoKhoi1 = radGridView9.Rows[i].Cells["SoKhoi_outputLS"].Value.ToString();
                                PhanLoai1 = radGridView9.Rows[i].Cells["PhanLoai_outputLS"].Value.ToString();
                                DVT1 = radGridView9.Rows[i].Cells["DVT_outputLS"].Value.ToString();
                                KhachHang1 = radGridView9.Rows[i].Cells["KhachHang_outputLS"].Value.ToString();
                                GhiChu1 = radGridView9.Rows[i].Cells["GhiChu_outputLS"].Value.ToString();
                                ID1 = radGridView9.Rows[i].Cells["Serial_outputLS"].Value.ToString();
                                cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                                cmd.Parameters.AddWithValue("@CDay", DVD1);
                                cmd.Parameters.AddWithValue("@CRong", DVD2);
                                cmd.Parameters.AddWithValue("@CDai", DVD3);
                                cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                                cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.Parameters.AddWithValue("@Wait", "0");
                                cmd.Parameters.AddWithValue("@ID", ID1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                radGridView9.Rows.RemoveAt(i);
                            }
                            else if (string_temp == "Van")
                            {
                                cmd.CommandText = "Trans_InsUp_NhapVan";
                                DVD1 = radGridView9.Rows[i].Cells["DVD1_outputLS"].Value.ToString();
                                // DVD2 = radGridView9.Rows[i].Cells["DVT2_outputLS"].Value.ToString();
                                //DVD3 = radGridView9.Rows[i].Cells["DVT3_outputLS"].Value.ToString();
                                SoLuong1 = double.Parse(radGridView9.Rows[i].Cells["SoLuong_outputLS"].Value.ToString()).ToString();
                                SoKhoi1 = radGridView9.Rows[i].Cells["SoKhoi_outputLS"].Value.ToString();
                                //PhanLoai1 = radGridView9.Rows[i].Cells["PhanLoai_outputLS"].Value.ToString();
                                DVT1 = radGridView9.Rows[i].Cells["DVT_outputLS"].Value.ToString();
                                KhachHang1 = radGridView9.Rows[i].Cells["KhachHang_outputLS"].Value.ToString();
                                GhiChu1 = radGridView9.Rows[i].Cells["GhiChu_outputLS"].Value.ToString();
                                ID1 = radGridView9.Rows[i].Cells["Serial_outputLS"].Value.ToString();
                                cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                                cmd.Parameters.AddWithValue("@CDay", DVD1);
                                cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                                cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@SoKhoiNhap", SoKhoi1);
                                cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                                cmd.Parameters.AddWithValue("@Wait", "0");
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.Parameters.AddWithValue("@ID", ID1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                radGridView9.Rows.RemoveAt(i);
                            }
                            else if (string_temp == "GoTron")
                            {
                                cmd.CommandText = "Trans_InsUp_NhapGT";
                                DVD1 = double.Parse(radGridView9.Rows[i].Cells["DVD1_outputLS"].Value.ToString()).ToString();
                                DVD2 = double.Parse(radGridView9.Rows[i].Cells["DVD2_outputLS"].Value.ToString()).ToString();
                                //DVD3 = radGridView9.Rows[i].Cells["DVT3_outputLS"].Value.ToString();
                                SoLuong1 = SoLuong1 = double.Parse(radGridView9.Rows[i].Cells["SoLuong_outputLS"].Value.ToString()).ToString();
                                //SoKhoi1 = radGridView9.Rows[i].Cells["SoKhoi_outputLS"].Value.ToString();
                                //PhanLoai1 = radGridView9.Rows[i].Cells["PhanLoai_outputLS"].Value.ToString();
                                DVT1 = radGridView9.Rows[i].Cells["DVT_outputLS"].Value.ToString();
                                KhachHang1 = radGridView9.Rows[i].Cells["KhachHang_outputLS"].Value.ToString();
                                GhiChu1 = radGridView9.Rows[i].Cells["GhiChu_outputLS"].Value.ToString();
                                ID1 = radGridView9.Rows[i].Cells["Serial_outputLS"].Value.ToString();

                                cmd.Parameters.AddWithValue("@Serial_ChungTu", Serial1);
                                cmd.Parameters.AddWithValue("@CDai", DVD2);
                                cmd.Parameters.AddWithValue("@Vanh", DVD1);
                                //cmd.Parameters.AddWithValue("@DVT", DVD3);
                                //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                cmd.Parameters.AddWithValue("@SLNhap", SoLuong1);
                                cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                                cmd.Parameters.AddWithValue("@Wait", "0");
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.Parameters.AddWithValue("@ID", ID1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                radGridView9.Rows.RemoveAt(i);
                            }

                        }
                        MessageBox.Show("Dữ liệu được lưu thành công");
                        bt_rework_outputLS_Click(null, null);
                    }
                    else
                    {
                        MessageBox.Show("Bạn cần nhập số chứng từ");
                    }
                }

                catch (Exception ex)
                {
                    string s = "insert duplicate key";
                    string FK = "FOREIGN KEY constraint";
                    string s1 = ex.Message.ToString();
                    if (s1.Contains(s))
                    { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                    else if (s1.Contains(FK))
                    { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                    else
                        MessageBox.Show(ex.Message);
                    DialogResult Err;
                    Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }

        }
        private void bt_inputKho_Click(object sender, EventArgs e)
        {
            NhapKho_LoSay();
        }
        private void LoSay_GiaoSX()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Tran_Ins_CTNhap";
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            //MessageBox.Show(radGridView9.Rows.Count.ToString());
            if (radGridView9.Rows.Count > 0)
            {
                try
                {

                    //string insert_NhapKTam = null;
                    if (txt_ChungTu_outputLS.Text != "")
                    {
                        NoiGiao1 = "Lo Say";
                        NoiNhan1 = "CO1";
                        ADorKD1 = "KD";
                        LoaiGo1 = cb_NguonGo_outputLS.Text.Trim();
                        LyDo1 = "GNLoSay";
                        BienBan1 = txt_BienBan_outputLS.Text.Trim();
                        ChungTu1 = txt_ChungTu_outputLS.Text.Trim();

                        Ngay1 = Date_Split(dtp_Date_outputLS.Text);

                        cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                        cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                        cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                        cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                        cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                        cmd.Parameters.AddWithValue("@SoXe", "");
                        cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                        cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                        SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                        code.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(code);
                        SqlDataReader sdr = cmd.ExecuteReader();
                        Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                        sdr.Close();
                        cmd.Parameters.Clear();
                        // Them 1 chung tu xuat
                        //MessageBox.Show(Serial1);
                        NoiGiao1 = "CO1";
                        NoiNhan1 = "San Xuat NT";
                        ADorKD1 = "KD";
                        LoaiGo1 = cb_NguonGo_outputLS.Text.Trim();
                        LyDo1 = "GSX";
                        BienBan1 = txt_BienBan_outputLS.Text.Trim();
                        ChungTu1 = txt_ChungTu_outputLS.Text.Trim();

                        Ngay1 = Date_Split(dtp_Date_outputLS.Text);

                        cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                        cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                        cmd.Parameters.AddWithValue("@ADorKD", ADorKD1);
                        cmd.Parameters.AddWithValue("@LoaiGo", LoaiGo1);
                        cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                        cmd.Parameters.AddWithValue("@BienBan", BienBan1);
                        cmd.Parameters.AddWithValue("@ChungTu", ChungTu1);
                        cmd.Parameters.AddWithValue("@SoXe", "");
                        cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                        cmd.Parameters.AddWithValue("@UserInsert", lb_Useraccount.Text);
                        SqlParameter code1 = new SqlParameter("@Serial", SqlDbType.Int);
                        code1.Direction = ParameterDirection.Output;
                        cmd.Parameters.Add(code1);
                        SqlDataReader sdr1 = cmd.ExecuteReader();
                        Serial2 = cmd.Parameters["@Serial"].Value.ToString();
                        sdr1.Close();
                        cmd.Parameters.Clear();
                        //MessageBox.Show(Serial2);
                        //MessageBox.Show("aaaa");


                        for (i = radGridView9.Rows.Count - 1; i >= 0; i--)
                        {
                            string_temp = radGridView9.Rows[i].Cells["Nhom_outputLS"].Value.ToString();
                            //MessageBox.Show(string_temp);
                            if (string_temp == "Phach")
                            {
                                cmd.CommandText = "Trans_InsUpIns_NhapPhach";
                                DVD1 = radGridView9.Rows[i].Cells["DVD1_outputLS"].Value.ToString();
                                DVD2 = radGridView9.Rows[i].Cells["DVD2_outputLS"].Value.ToString();
                                DVD3 = radGridView9.Rows[i].Cells["DVD3_outputLS"].Value.ToString();
                                SoLuong1 = double.Parse(radGridView9.Rows[i].Cells["SoLuong_outputLS"].Value.ToString()).ToString();
                                //SoKhoi1 = radGridView9.Rows[i].Cells["SoKhoi_outputLS"].Value.ToString();
                                PhanLoai1 = radGridView9.Rows[i].Cells["PhanLoai_outputLS"].Value.ToString();
                                DVT1 = radGridView9.Rows[i].Cells["DVT_outputLS"].Value.ToString();
                                KhachHang1 = radGridView9.Rows[i].Cells["KhachHang_outputLS"].Value.ToString();
                                GhiChu1 = radGridView9.Rows[i].Cells["GhiChu_outputLS"].Value.ToString();
                                ID1 = radGridView9.Rows[i].Cells["Serial_outputLS"].Value.ToString();
                                cmd.Parameters.AddWithValue("@Serial_ChungTu1", Serial1);
                                cmd.Parameters.AddWithValue("@Serial_ChungTu2", Serial2);
                                cmd.Parameters.AddWithValue("@CDay", DVD1);
                                cmd.Parameters.AddWithValue("@CRong", DVD2);
                                cmd.Parameters.AddWithValue("@CDai", DVD3);
                                cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                cmd.Parameters.AddWithValue("@SoLuong", SoLuong1);
                                //cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                                cmd.Parameters.AddWithValue("@Wait", "0");
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.Parameters.AddWithValue("@ID", ID1);

                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                radGridView9.Rows.RemoveAt(i);
                            }
                            else if (string_temp == "Van")
                            {
                                cmd.CommandText = "Trans_InsUpIns_NhapVan";
                                DVD1 = radGridView9.Rows[i].Cells["DVD1_outputLS"].Value.ToString();
                                // DVD2 = radGridView9.Rows[i].Cells["DVT2_outputLS"].Value.ToString();
                                //DVD3 = radGridView9.Rows[i].Cells["DVT3_outputLS"].Value.ToString();
                                SoLuong1 = double.Parse(radGridView9.Rows[i].Cells["SoLuong_outputLS"].Value.ToString()).ToString();
                                SoKhoi1 = radGridView9.Rows[i].Cells["SoKhoi_outputLS"].Value.ToString();
                                //PhanLoai1 = radGridView9.Rows[i].Cells["PhanLoai_outputLS"].Value.ToString();
                                DVT1 = radGridView9.Rows[i].Cells["DVT_outputLS"].Value.ToString();
                                KhachHang1 = radGridView9.Rows[i].Cells["KhachHang_outputLS"].Value.ToString();
                                GhiChu1 = radGridView9.Rows[i].Cells["GhiChu_outputLS"].Value.ToString();
                                ID1 = radGridView9.Rows[i].Cells["Serial_outputLS"].Value.ToString();
                                cmd.Parameters.AddWithValue("@Serial_ChungTu1", Serial1);
                                cmd.Parameters.AddWithValue("@Serial_ChungTu2", Serial2);
                                cmd.Parameters.AddWithValue("@CDay", DVD1);
                                cmd.Parameters.AddWithValue("@SoLuong", SoLuong1);
                                //cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@SoKhoi", SoKhoi1);
                                //cmd.Parameters.AddWithValue("@SoKhoiXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                                cmd.Parameters.AddWithValue("@Wait", "0");
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.Parameters.AddWithValue("@ID", ID1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                radGridView9.Rows.RemoveAt(i);
                            }
                            else if (string_temp == "GoTron")
                            {
                                cmd.CommandText = "Trans_InsUpIns_NhapGT";
                                DVD1 = double.Parse(radGridView9.Rows[i].Cells["DVD1_outputLS"].Value.ToString()).ToString();
                                DVD2 = double.Parse(radGridView9.Rows[i].Cells["DVD2_outputLS"].Value.ToString()).ToString();
                                //DVD3 = radGridView9.Rows[i].Cells["DVT3_outputLS"].Value.ToString();
                                SoLuong1 = SoLuong1 = double.Parse(radGridView9.Rows[i].Cells["SoLuong_outputLS"].Value.ToString()).ToString();
                                //SoKhoi1 = radGridView9.Rows[i].Cells["SoKhoi_outputLS"].Value.ToString();
                                //PhanLoai1 = radGridView9.Rows[i].Cells["PhanLoai_outputLS"].Value.ToString();
                                DVT1 = radGridView9.Rows[i].Cells["DVT_outputLS"].Value.ToString();
                                KhachHang1 = radGridView9.Rows[i].Cells["KhachHang_outputLS"].Value.ToString();
                                GhiChu1 = radGridView9.Rows[i].Cells["GhiChu_outputLS"].Value.ToString();
                                ID1 = radGridView9.Rows[i].Cells["Serial_outputLS"].Value.ToString();

                                cmd.Parameters.AddWithValue("@Serial_ChungTu1", Serial1);
                                cmd.Parameters.AddWithValue("@Serial_ChungTu2", Serial2);
                                cmd.Parameters.AddWithValue("@CDai", DVD2);
                                cmd.Parameters.AddWithValue("@Vanh", DVD1);
                                //cmd.Parameters.AddWithValue("@DVT", DVD3);
                                //cmd.Parameters.AddWithValue("@PhanLoai", PhanLoai1);
                                cmd.Parameters.AddWithValue("@SoLuong", SoLuong1);
                                //cmd.Parameters.AddWithValue("@SLXuat", "0");
                                cmd.Parameters.AddWithValue("@DVT", DVT1);
                                cmd.Parameters.AddWithValue("@KhachHang", KhachHang1);
                                cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);

                                cmd.Parameters.AddWithValue("@Wait", "0");
                                cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                                cmd.Parameters.AddWithValue("@ID", ID1);
                                cmd.ExecuteNonQuery();
                                cmd.Parameters.Clear();
                                radGridView9.Rows.RemoveAt(i);
                            }

                        }
                        MessageBox.Show("Dữ liệu được lưu thành công");
                        bt_rework_outputLS_Click(null, null);
                        
                    }
                    else
                    {
                        MessageBox.Show("Bạn cần nhập số chứng từ");
                    }
                }

                catch (Exception ex)
                {
                    string s = "insert duplicate key";
                    string FK = "FOREIGN KEY constraint";
                    string s1 = ex.Message.ToString();
                    if (s1.Contains(s))
                    { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                    else if (s1.Contains(FK))
                    { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                    else
                        MessageBox.Show(ex.Message);
                    DialogResult Err;
                    Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                }
            }
        }
        private void bt_outputSX_Click(object sender, EventArgs e)
        {
            LoSay_GiaoSX();
        }
        private void rd_GoTron_BCLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            lb_DVD1_BCLS.Text = "C Dài";
            lb_DVD2_BCLS.Text = "Vanh";
            lb_DVD3_BCLS.Text = "";
            radGridView10.Columns["DVD1_BCLS"].HeaderText = "C Dài";
            radGridView10.Columns["DVD2_BCLS"].HeaderText = "Vanh";
            radGridView10.Columns["DVD3_BCLS"].HeaderText = "";
        }

        private void rd_Phach_BCLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            lb_DVD1_BCLS.Text = "C Dày";
            lb_DVD2_BCLS.Text = "C Rộng";
            lb_DVD3_BCLS.Text = "C Dài";
            radGridView10.Columns["DVD1_BCLS"].HeaderText = "C Dày";
            radGridView10.Columns["DVD2_BCLS"].HeaderText = "C Rộng";
            radGridView10.Columns["DVD3_BCLS"].HeaderText = "C Dài";
        }
        private void rd_Van_BCLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            lb_DVD1_BCLS.Text = "C Dày";
            lb_DVD2_BCLS.Text = "";
            lb_DVD3_BCLS.Text = "";
            radGridView10.Columns["DVD1_BCLS"].HeaderText = "C Dày";
            radGridView10.Columns["DVD2_BCLS"].HeaderText = "";
            radGridView10.Columns["DVD3_BCLS"].HeaderText = "";
        }

        private void bt_listcheck_Click(object sender, EventArgs e)
        {
            if (radGridView9.Rows.Count > 0)
            {
                for (i = radGridView9.Rows.Count - 1; i >= 0; i--)
                {
                    if (radGridView9.Rows[i].Cells["Check_outputLS"].Value.ToString() == "False")
                    {
                        radGridView9.Rows.RemoveAt(i);
                    }

                }
                bt_inputKho.Enabled = true;
                bt_outputSX.Enabled = true;
            }
        }

        private void bt_rework_outputLS_Click(object sender, EventArgs e)
        {
            radGridView9.Rows.Clear();
            txt_Serial_outputLS.Text = "";
            bt_outputSX.Enabled = false;
            bt_inputKho.Enabled = false;
        }
        private void bt_Search_outputLS_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            Conn.Open();
            DataSet ds = new DataSet();
            try
            {
                string_temp = @"SELECT [NoiNhan],NoiGiao,[DVD1] as CDay,[DVD2] as CRong,[DVD3] as CDai,[SLNhap],[SoKhoiNhap],[ADorKD],[PhanLoai],[LoaiGo] ,[LyDo],[BienBan],[ChungTu]
                                  ,convert(varchar(10),[NgayBegin],103) as NgayBegin,convert(varchar(10),[NgayEnd],103) as NgayEnd,[ID],[GhiChu]
                                  ,[Nhom],[Serial],[Wait]
                            FROM [dbo].[NhapGoLS] where Wait=1";
                if (cb_LoSay_BCLS.Text == "")
                {
                    if (rd_Phach_BCLS.IsChecked == true)
                    {
                        string_temp += @" and Nhom='Phach'";
                    }
                    if (rd_Van_BCLS.IsChecked == true)
                    {
                        string_temp += @" and Nhom='Van'";
                    }
                    if (rd_GoTron_BCLS.IsChecked == true)
                    {
                        string_temp += @" and Nhom='GoTron'";
                    }
                    if (txt_DVD1_BCLS.Text != "")
                    {
                        string_temp += @" and DVD1=" + txt_DVD1_BCLS.Text;

                    }
                }
                else
                {
                    string_temp += " and NoiNhan='" + cb_LoSay_BCLS.Text + "'" ;
                    radGridView10.Columns["DVD1_BCLS"].HeaderText = "DVD 1";
                    radGridView10.Columns["DVD2_BCLS"].HeaderText = "DVD 2";
                    radGridView10.Columns["DVD3_BCLS"].HeaderText = "DVD 3";
                }
                if (txt_DVD2_BCLS.Text != "")
                {
                    string_temp += @" and DVD2=" + txt_DVD2_BCLS.Text;

                }
                if (txt_DVD3_BCLS.Text != "")
                {
                    string_temp += @" and DVD3=" + txt_DVD3_BCLS.Text;
                }
                if (radGridView10.Rows.Count > 0)
                {
                    for (i = 0; i < this.radGridView10.ChildRows.Count; i++)
                    {
                        GridViewGroupRowInfo groupRowInfo = this.radGridView10.ChildRows[i] as GridViewGroupRowInfo;
                        groupRowInfo.HeaderText = "";
                    }
                }
                ds = prs.dts(string_temp, Conn, ds);
                radGridView10.DataSource = ds.Tables[0];
                if (ds.Tables[0].Rows.Count > 0)
                    {
                        SLNhap_d = 0;
                       
                        //count = 0;
                        SoKhoi_d = 0;
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {	
                            SLNhap_d += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                            //SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoi"].ToString());
                            SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SoKhoiNhap"].ToString());
                        }
                        lb_SLNhap_BCLS.Text = string.Format("{0:###,###,###.#}", SLNhap_d);
                        lb_SoKhoiNhap_BCLS.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                        //lb_SLPallet_KV5Search.Text = string.Format("{0:###,###}", i);
                        for (i = 0; i < this.radGridView10.ChildRows.Count; i++)
                        {
                            //this.radGridView10.ChildRows[i].Cells["Serial_KV5Search"].Value = i.ToString();

                            GridViewGroupRowInfo groupRowInfo = this.radGridView10.ChildRows[i] as GridViewGroupRowInfo;
                            if (groupRowInfo != null)
                            {
                                SLNhap_d = 0;
                                SoKhoi_d = 0;
                                //groupRowInfo.HeaderText = groupRowInfo.HeaderText + " " + i.ToString();
                                for (int p = 0; p < this.radGridView10.ChildRows[i].ChildRows.Count; p++)
                                {
                                    //this.radGridView10.ChildRows[i].ChildRows[p].Cells["Serial_KV5Search"].Value = p.ToString();
                                    SLNhap_d += double.Parse(radGridView10.ChildRows[i].ChildRows[p].Cells["SoLuong_BCLS"].Value.ToString());
                                    SoKhoi_d += double.Parse(radGridView10.ChildRows[i].ChildRows[p].Cells["SoKhoi_BCLS"].Value.ToString());
                                }
                                string_temp = groupRowInfo.HeaderText + "            Số lượng: " + string.Format("{0:###,###,###}",SLNhap_d)+ "            Số khối : " + string.Format("{0:###,###.####}",SoKhoi_d);
                                groupRowInfo.HeaderText = string_temp;

                            }
                        }
                    }
                    else
                        MessageBox.Show("Không có dữ liệu");

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                //ds.Clear();
            }
        }
        private void bt_Exportexcel_BCLS_Click(object sender, EventArgs e)
        {
            if (radGridView10.Rows.Count > 1)
            {
                DataTable dt = (DataTable)radGridView10.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\KhoGo_LoSay.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }
        private void bt_PrintReport_BCLS_Click(object sender, EventArgs e)
        {
            try
            {
                //DataSet ds = new DataSet();
                DataTable dt = new DataTable();
                if (radGridView10.Rows.Count > 0)
                {
                    dt = (DataTable)radGridView10.DataSource;
                    Fr_Report FrR = new Fr_Report();
                    //string_temp = "Từ ngày: " + dtp_Datebegin_BCKG.Text + " Đến ngày: " + dtp_Dateend_BCKG.Text;
                    if (rd_Phach_BCKG.IsChecked == true || rd_Van_BCKG.IsChecked == true)
                        FrR.CrR_GoDangSay(dt);
                    if (rd_GoTron_BCKG.IsChecked == true)
                        FrR.CrR_GoTron_ReportKG(ds, string_temp, DVT1);
                    // FrR.report_KV1NKTG(ds, Ngay1);
                    FrR.Show();
                    ds.Clear();
                }
                else
                {
                    MessageBox.Show("Danh sách dữ liệu không tồn tại");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void print_TH_outputLS()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                Ngay1 = Date_Split(dtp_Date_outputLS.Text);

                string_temp = @" SELECT [NoiGiao],[NoiNhan],[ADorKD],[LoaiGo] ,[LyDo] ,[BienBan] as SoBB,[ChungTu],[DVD1],[DVD2],[DVD3],[PhanLoai],[SLNhap] as SLXuat,[SoKhoiNhap] as SoKhoiXuat
                                     ,[KhachHang],[GhiChu],[Serial_ChungTu] as Serial
                                    FROM [dbo].[NhapGoLS] where Wait=0 ";

               
                if (cb_NoiNhan_outputLS.Text != "")
                    GhiChu1 += " and NoiNhan='" + prs.substring_left(cb_NoiNhan_outputLS.Text, child) + "'";
                /*if (cb_LoaiGo_outputLS.Text != "")
                    GhiChu1 += " and LoaiGo='" + cb_LoaiGo_outputLS.Text + "'";*/
                
                if (txt_ChungTu_outputLS.Text != "")
                    GhiChu1 += " and ChungTu='" + txt_ChungTu_outputLS.Text + "'";
                if (txt_BienBan_outputLS.Text != "")
                    GhiChu1 += " and BienBan='" + txt_BienBan_outputLS.Text + "'";
               
                string_temp += GhiChu1 + " and NgayRaLo='" + Ngay1 + "'";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();
                Ngay1 = "Ngày ra lò: " + dtp_Date_outputLS.Text;

                // FrR.report_KV1NKTG(ds, Ngay1);


                FrR.CrR_LS_XuatKho(ds, Ngay1);


                FrR.Show();
                ds.Clear();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        }

        #endregion
        #region //Bang Ke Lo Say
        private DataSet BangKe_LS()
        {
            DataSet ds = new DataSet();
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            Ngay1 = "";
            Datebegin = Date_Split(dtp_Datebegin_BKLS.Text) + " 00:00";
            Dateend = Date_Split(dtp_Dateend_BKLS.Text) + " 23:59";
            if (rd_Phach_BKLS.IsChecked == true)
            {
                string_temp = @"select dbo.ChungTuLoSay.Serial,dbo.ChungTuLoSay.ChungTu,dbo.Nhap_PhachLS.CDay as DVD1,dbo.Nhap_PhachLS.CRong as DVD2,dbo.Nhap_PhachLS.CDai as DVD3,
                            dbo.Nhap_PhachLS.SLNhap,dbo.Nhap_PhachLS.DVT,dbo.Nhap_PhachLS.CDai*CRong*CDay*SLNhap/1000000000 as SKNhap,
                            dbo.Nhap_PhachLS.PhanLoai,dbo.ChungTuLoSay.ADorKD,dbo.ChungTuLoSay.LoaiGo,dbo.ChungTuLoSay.NoiGiao,dbo.ChungTuLoSay.NoiNhan,dbo.ChungTuLoSay.LyDo,
                            dbo.ChungTuLoSay.BienBan,dbo.ChungTuLoSay.NgayBegin as Ngay,dbo.Nhap_PhachLS.NgayInsert,dbo.Nhap_PhachLS.GhiChu,dbo.Nhap_PhachLS.Wait
                            from dbo.Nhap_PhachLS inner join dbo.ChungTuLoSay on (dbo.ChungTuLoSay.Serial=dbo.Nhap_PhachLS.Serial_ChungTu";
                if (txt_DVD1_BKLS.Text != "")
                    GhiChu1 += " and CDay=" + txt_DVD1_BKLS.Text;
                if (txt_DVD2_BKLS.Text != "")
                    GhiChu1 += " and CRong=" + txt_DVD2_BKLS.Text;
                if (txt_DVD3_BKLS.Text != "")
                    GhiChu1 += " and CDai=" + txt_DVD3_BKLS.Text;
                if ((chk_Nhap_BKLS.Checked == true && chk_Xuat_BKLS.Checked == true) || (chk_Nhap_BKLS.Checked == false && chk_Xuat_BKLS.Checked == false))
                {
                    MessageBox.Show("Bạn muốn xem theo vào lò hay ra lò");
                    chk_Xuat_BKLS.Checked = false;
                    chk_Nhap_BKLS.Checked = false;
                }
                else
                {
                    if (chk_Nhap_BKLS.Checked == true)
                    {
                        if(chk_NgayCTLS.Checked==false &&chk_NgayPS_LS.Checked==true)
                            Ngay1 += " and dbo.Nhap_PhachLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_PhachLS.NgayInsert<='" + Dateend + "'";
                        if (chk_NgayCTLS.Checked == true && chk_NgayPS_LS.Checked == false)
                            Ngay1 += " and dbo.ChungTuLoSay.NgayBegin>='" + Datebegin + "' and dbo.ChungTuLoSay.NgayBegin<='" + Dateend + "'";

                    }
                    if (chk_Xuat_BKLS.Checked == true)
                    {
                        if (chk_NgayCTLS.Checked == false && chk_NgayPS_LS.Checked == true)
                            Ngay1 += " and dbo.Nhap_PhachLS.NgayXuat>='" + Datebegin + "' and dbo.Nhap_PhachLS.NgayXuat<='" + Dateend + "'";
                        if (chk_NgayCTLS.Checked == true && chk_NgayPS_LS.Checked == false)
                            Ngay1 += " and dbo.Nhap_PhachLS.NgayRaLo>='" + Datebegin + "' and dbo.Nhap_PhachLS.NgayRaLo<='" + Dateend + "'";
                    }
                }
                
            }
            if (rd_Van_BKLS.IsChecked == true)
            {
                string_temp = @"select dbo.ChungTuLoSay.Serial,dbo.ChungTuLoSay.ChungTu,dbo.Nhap_VanLS.CDay as DVD1,'' as DVD2,'' as DVD3,
                                dbo.Nhap_VanLS.SLNhap,dbo.Nhap_VanLS.DVT,SoKhoiNhap as SKNhap,
                                '' as PhanLoai,dbo.ChungTuLoSay.ADorKD,dbo.ChungTuLoSay.LoaiGo,dbo.ChungTuLoSay.NoiGiao,dbo.ChungTuLoSay.NoiNhan,dbo.ChungTuLoSay.LyDo,
                                dbo.ChungTuLoSay.BienBan,convert(varchar(10),dbo.ChungTuLoSay.NgayBegin,103) as Ngay,dbo.Nhap_VanLS.NgayInsert,dbo.Nhap_VanLS.GhiChu,dbo.Nhap_VanLS.Wait
                                from dbo.Nhap_VanLS inner join dbo.ChungTuLoSay on (dbo.ChungTuLoSay.Serial=dbo.Nhap_VanLS.Serial_ChungTu";
                if (txt_DVD1_BKLS.Text != "")
                    GhiChu1 += " and CDay=" + txt_DVD1_BKLS.Text;
                //Ngay1 += " and dbo.Nhap_VanLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_VanLS.NgayInsert<='" + Dateend + "'";
                if ((chk_Nhap_BKLS.Checked == true && chk_Xuat_BKLS.Checked == true) || (chk_Nhap_BKLS.Checked == false && chk_Xuat_BKLS.Checked == false))
                {
                    //Ngay1 += " and ((dbo.Nhap_VanLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_VanLS.NgayInsert<='" + Dateend + "') or (dbo.Nhap_VanLS.NgayXuat>='" + Datebegin + "' and dbo.Nhap_VanLS.NgayXuat<='" + Dateend + "')))";

                    MessageBox.Show("Bạn muốn xem theo nhập hay xuất");
                    chk_Xuat_BKLS.Checked = false;
                    chk_Nhap_BKLS.Checked = false;
                }
                else
                {
                    if (chk_Nhap_BKLS.Checked == true)
                    {
                        if (chk_NgayCTLS.Checked == false && chk_NgayPS_LS.Checked == true)
                            Ngay1 += " and dbo.Nhap_VanLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_VanLS.NgayInsert<='" + Dateend + "'";
                        if (chk_NgayCTLS.Checked == true && chk_NgayPS_LS.Checked == false)
                            Ngay1 += " and dbo.ChungTuLoSay.NgayBegin>='" + Datebegin + "' and dbo.ChungTuLoSay.NgayBegin<='" + Dateend + "'";

                    }
                    if (chk_Xuat_BKLS.Checked == true)
                    {
                        if (chk_NgayCTLS.Checked == false && chk_NgayPS_LS.Checked == true)
                            Ngay1 += " and dbo.Nhap_VanLS.NgayXuat>='" + Datebegin + "' and dbo.Nhap_VanLS.NgayXuat<='" + Dateend + "'";
                        if (chk_NgayCTLS.Checked == true && chk_NgayPS_LS.Checked == false)
                            Ngay1 += " and dbo.Nhap_VanLS.NgayRaLo>='" + Datebegin + "' and dbo.Nhap_VanLS.NgayRaLo<='" + Dateend + "'";
                    }
                }

            }
            if (rd_GoTron_BKLS.IsChecked == true)
            {
                string_temp = @"select dbo.ChungTuLoSay.Serial,dbo.ChungTuLoSay.ChungTu,dbo.Nhap_GoTronLS.CDai as DVD1,dbo.Nhap_GoTronLS.Vanh as DVD2,'' as DVD3,
                            dbo.Nhap_GoTronLS.SLNhap,dbo.Nhap_GoTronLS.DVT,(Vanh * Vanh * CDai * SLNhap) 
                                             / (4 * 3.1415926 * 1000000000) AS SKNhap,
                            '' as PhanLoai,dbo.ChungTuLoSay.ADorKD,dbo.ChungTuLoSay.LoaiGo,dbo.ChungTuLoSay.NoiGiao,dbo.ChungTuLoSay.NoiNhan,dbo.ChungTuLoSay.LyDo,
                            dbo.ChungTuLoSay.BienBan,convert(varchar(10),dbo.ChungTuLoSay.NgayBegin,103) as Ngay,dbo.Nhap_GoTronLS.NgayInsert,dbo.Nhap_GoTronLS.GhiChu,dbo.Nhap_GoTronLS.Wait
                            from dbo.Nhap_GoTronLS inner join dbo.ChungTuLoSay on (dbo.ChungTuLoSay.Serial=dbo.Nhap_GoTronLS.Serial_ChungTu";
                if (txt_DVD1_BKLS.Text != "")
                    GhiChu1 += " and Dai=" + txt_DVD1_BKLS.Text;
                if (txt_DVD2_BKLS.Text != "")
                    GhiChu1 += " and Vanh=" + txt_DVD2_BKLS.Text;
                //Ngay1 += " and dbo.Nhap_GoTronLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_GoTronLS.NgayInsert<='" + Dateend + "'";
                if ((chk_Nhap_BKLS.Checked == true && chk_Xuat_BKLS.Checked == true) || (chk_Nhap_BKLS.Checked == false && chk_Xuat_BKLS.Checked == false))
                {
                   // Ngay1 += " and ((dbo.Nhap_GoTronLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_GoTronLS.NgayInsert<='" + Dateend + "') or (dbo.Nhap_GoTronLS.NgayXuat>='" + Datebegin + "' and dbo.Nhap_GoTronLS.NgayXuat<='" + Dateend + "')))";

                    MessageBox.Show("Bạn muốn xem theo nhập hay xuất");
                    chk_Xuat_BKLS.Checked = false;
                    chk_Nhap_BKLS.Checked = false;
                    
                }
                else
                {
                    if (chk_Nhap_BKLS.Checked == true)
                    {
                        if (chk_NgayCTLS.Checked == false && chk_NgayPS_LS.Checked == true)
                            Ngay1 += " and dbo.Nhap_GoTronLS.NgayInsert>='" + Datebegin + "' and dbo.Nhap_GoTronLS.NgayInsert<='" + Dateend + "'";
                        if (chk_NgayCTLS.Checked == true && chk_NgayPS_LS.Checked == false)
                            Ngay1 += " and dbo.ChungTuLoSay.NgayBegin>='" + Datebegin + "' and dbo.ChungTuLoSay.NgayBegin<='" + Dateend + "'";

                    }
                    if (chk_Xuat_BKLS.Checked == true)
                    {
                        if (chk_NgayCTLS.Checked == false && chk_NgayPS_LS.Checked == true)
                            Ngay1 += " and dbo.Nhap_GoTronLS.NgayXuat>='" + Datebegin + "' and dbo.Nhap_GoTronLS.NgayXuat<='" + Dateend + "'";
                        if (chk_NgayCTLS.Checked == true && chk_NgayPS_LS.Checked == false)
                            Ngay1 += " and dbo.Nhap_GoTronLS.NgayRaLo>='" + Datebegin + "' and dbo.Nhap_GoTronLS.NgayRaLo<='" + Dateend + "'";
                    }
                }

            }


           

            string_temp += GhiChu1 + Ngay1 + ")";
            ds = prs.dts(string_temp, Conn, ds);
            Conn.Close();
            return ds;
        }

        private void BangKeLS()
        {
        }
        private void bt_Search_BKLS_Click(object sender, EventArgs e)
        {
            try
            {
                if ((chk_Nhap_BKLS.Checked == true && chk_Xuat_BKLS.Checked == true) || (chk_Nhap_BKLS.Checked == false && chk_Xuat_BKLS.Checked == false))
                {
                    MessageBox.Show("Bạn muốn xem theo Vào lò hay Ra lò");
                }
                else
                {
                    DataSet ds = BangKe_LS();
                    radGridView11.DataSource = ds.Tables[0];
                    SLNhap_d = 0;
                    //SLXuat_d = 0;
                    SoKhoi_d = 0;
                    temp_double = 0;
                    if (chk_Nhap_BKLS.Checked == true)
                    {
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            SLNhap_d += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                            SoKhoi_d += double.Parse(ds.Tables[0].Rows[i]["SKNhap"].ToString());

                            //SLXuat_d += double.Parse(ds.Tables[0].Rows[i]["SLXuat"].ToString());


                            if (ds.Tables[0].Rows[i]["Wait"].ToString() == "0")
                                temp_double += double.Parse(ds.Tables[0].Rows[i]["SKNhap"].ToString());

                        }
                    }
                    if (chk_Xuat_BKLS.Checked == true)
                    {
                        SoKhoi_d = 0;
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            SLNhap_d += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                            

                            //SLXuat_d += double.Parse(ds.Tables[0].Rows[i]["SLXuat"].ToString());


                            if (ds.Tables[0].Rows[i]["Wait"].ToString() == "0")
                                temp_double += double.Parse(ds.Tables[0].Rows[i]["SKNhap"].ToString());

                        }
                    }

                    lb_SLNhap_BKLS.Text = string.Format("{0:###,###,###}", SLNhap_d);
                    //lb_SLXuat_BKLS.Text = string.Format("{0:###,###,###}", SLXuat_d);
                    lb_SKNhap_BKLS.Text = string.Format("{0:###,###.###}", SoKhoi_d);
                    lb_SKXuat_BKLS.Text = string.Format("{0:###,###.###}", temp_double);
                    if (ds.Tables[0].Rows.Count == 0)
                        MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra thông tin tìm kiếm");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        
        }
        private void rd_Phach_BKLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView11.Columns["DVD1_BKLS"].HeaderText = "C Dày";
            radGridView11.Columns["DVD2_BKLS"].HeaderText = "C Rộng";
            radGridView11.Columns["DVD3_BKLS"].HeaderText = " C Dài";
        }

        private void rd_Van_BKLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView11.Columns["DVD1_BKLS"].HeaderText = "C Dày";
            radGridView11.Columns["DVD2_BKLS"].HeaderText = "";
            radGridView11.Columns["DVD3_BKLS"].HeaderText = " ";
        }

        private void rd_GoTron_BKLS_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            radGridView11.Columns["DVD1_BKLS"].HeaderText = "C Dài";
            radGridView11.Columns["DVD2_BKLS"].HeaderText = "Vanh";
            radGridView11.Columns["DVD3_BKLS"].HeaderText = " ";
        }
        private void bt_exportexcel_BKLS_Click(object sender, EventArgs e)
        {
            if (radGridView11.Rows.Count > 0)
            {
                DataTable dt = (DataTable)radGridView11.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\LoSay_BangKe.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }
        #endregion

        private void lnk_Black_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ChangeThemeName(this, lnk_Black.Text);
        }

        private void lnk_Blue_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ChangeThemeName(this, lnk_Blue.Text);
        }

        #region //Them Thong Tin

        private void bt_save_NGN_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            Conn.Open();
            SqlCommand cmd=new SqlCommand();
            cmd.Connection=Conn;
           
            try
            {
                if (txt_MaNGN_CN.Text == "" || txt_TenNGN_CN.Text == "")
                {
                    MessageBox.Show("Nhập thiếu thông tin");
                }
                else
                {
                    cmd.CommandText = "INSERT INTO [dbo].[NoiGiaoNhan]([MaNGN],[TenNGN])VALUES('" + txt_MaNGN_CN.Text.Trim() + "',N'" + txt_TenNGN_CN.Text.Trim() + "')";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    txt_TenNGN_CN.Text = "";
                    txt_MaNGN_CN.Text = "";
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_save_QC_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                if (txt_CDay_QC.Text == "" || txt_CRong_QC.Text == "" || txt_CDai_QC.Text == "")
                {
                    MessageBox.Show("Nhập thiếu thông tin");
                }
                else
                {
                    ID1 = txt_CDay_QC.Text.Trim() + txt_CRong_QC.Text.Trim() + txt_CDai_QC.Text.Trim();
                    cmd.CommandText = "INSERT INTO [dbo].[QuyCach]([QuyCach],[CDay],[CRong],[CDai],[PhanLoai])VALUES('" + ID1 + "'," + txt_CDay_QC.Text.Trim() + "," + txt_CRong_QC.Text.Trim() + "," + txt_CDai_QC.Text.Trim() + ",N'"+cb_PhanLoai_QC.Text.Trim()+"')";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    string_temp = ID1 + " - " + txt_CDay_QC.Text.Trim() +"x"+ txt_CRong_QC.Text.Trim()+"x" + txt_CDai_QC.Text.Trim();
                    cb_QuyCach_inputKG.Items.Add(string_temp);
                    cb_QuyCach_outputKG.Items.Add(string_temp);
                    cb_QuyCach_inputLS.Items.Add(string_temp);
                    dataGridView_QuyCach.Rows.Add(ID1, txt_CDay_QC.Text.Trim() + "x" + txt_CRong_QC.Text.Trim() + "x" + txt_CDai_QC.Text.Trim(), txt_CDay_QC.Text, txt_CRong_QC.Text, txt_CDai_QC.Text, cb_PhanLoai_QC.Text);
                    txt_CDay_QC.Text = "";
                    txt_CRong_QC.Text = "";
                    txt_CDai_QC.Text = "";
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_saveLyDo_CN_Click(object sender, EventArgs e)
        {
            Conn = prs.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            try
            {
                if (txt_MaLyDo_CN.Text == "" || txt_TenLyDo_CN.Text == "")
                {
                    MessageBox.Show("Nhập thiếu thông tin");
                }
                else
                {
                    //ID1 = txt_CDay_QC.Text.Trim() + txt_CRong_QC.Text.Trim() + txt_CDai_QC.Text.Trim();
                    cmd.CommandText = "INSERT INTO [dbo].[LyDo]([MaLyDo],[LyDo])VALUES('" + txt_MaLyDo_CN.Text.Trim() + "',N'" + txt_TenLyDo_CN.Text.Trim() + "')";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Dữ liệu được lưu thành công");
                    txt_MaLyDo_CN.Text = "";
                    txt_TenLyDo_CN.Text = "";
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        #endregion

        private void bt_exportexcel_BCKG_Click(object sender, EventArgs e)
        {

        }

       
        #region //In put NVL
        private void txt_SoLuong_inputNVL_Leave(object sender, EventArgs e)
        {
            prs.checktext_number(txt_SoLuong_inputNVL);
        }
        private void add_grid_inputNVL()
        {
            try
            {
                if (cb_TenSP_inputNVL.Text == "" || cb_LyDo_inputNVL.Text == "" || cb_NoiGiao_inputNVL.Text == "" || cb_NoiNhan_inputNVL.Text == "" || txt_SoLuong_inputNVL.Text == "" || txt_SoPhieu_inputNVL.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin, vui lòng kiểm tra lại");
                else
                {
                   
                    MaSP1 = prs.substring_left(cb_TenSP_inputNVL.Text, child);
                    TenSP1 = prs.substring_right(cb_TenSP_inputNVL.Text, child);
                    
                    if (MaSP1 == "" || TenSP1 == "")
                        MessageBox.Show("Nhập sai thông tin, vui lòng kiểm tra lại");
                    else
                    {


                        SoLuong_d = 0;
                        temp_double = 0;
                        count = 0;
                        SLNhap_d = double.Parse(txt_SoLuong_inputNVL.Text);
                        //DVT1 = cb_DVT_inputNVL.Text.Trim();
                        GhiChu1 = txt_GhiChu_inputNVL.Text.Trim();
                        if (rd_BaoBi_inputNVL.IsChecked == true)
                            Nhom1 = "BaoBi";
                        if (rd_DauMau_inputNVL.IsChecked == true)
                            Nhom1 = "DauMau";
                        if (rd_Fitting_inputNVL.IsChecked == true)
                            Nhom1 = "Fitting";
                        for (i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                        {
                            string_temp = dataGridView1.Rows[i].Cells["MaSP"].Value.ToString();
                            PhanLoai1 = dataGridView1.Rows[i].Cells["Nhom"].Value.ToString().Trim();
                           // MessageBox.Show(string_temp + " " + Nhom1);
                            if (MaSP1 == string_temp&&PhanLoai1==Nhom1)
                            {
                                string_temp = dataGridView1.Rows[i].Cells["MaChiTiet"].Value.ToString();
                                TenSP1 = dataGridView1.Rows[i].Cells["TenHang"].Value.ToString();
                                SoLuong1 = (double.Parse(dataGridView1.Rows[i].Cells["SLQuyDoi"].Value.ToString())*SLNhap_d).ToString();
                                DonGia1 = dataGridView1.Rows[i].Cells["DonGia"].Value.ToString();
                                DVT1 = dataGridView1.Rows[i].Cells["DVT"].Value.ToString();
                                ThanhTien_d = double.Parse(DonGia1) * double.Parse(SoLuong1);
                                radGridView12.Rows.Add(string_temp, TenSP1, SoLuong1, DonGia1, ThanhTien_d, DVT1, GhiChu1);
                                SoLuong_d += double.Parse(SoLuong1);
                                temp_double += ThanhTien_d;
                                count++;
                            }
                        }
                        if (count == 0)
                        {
                            if(txt_DonGia_inputNVL.Text=="")
                                DonGia1="0";
                            else
                                DonGia1=txt_DonGia_inputNVL.Text.Trim();
                            SoLuong_d = double.Parse(txt_SoLuong_inputNVL.Text);
                            ThanhTien_d = double.Parse(DonGia1) * double.Parse(txt_SoLuong_inputNVL.Text);
                            temp_double += ThanhTien_d;
                            DVT1 = cb_DVT_inputNVL.Text;
                            radGridView12.Rows.Add(MaSP1, TenSP1, txt_SoLuong_inputNVL.Text,DonGia1 , ThanhTien_d, DVT1, GhiChu1);
                        }
                        
                        SoLuong_d = SoLuong_d + double.Parse(lb_Tong_inputNVL.Text);
                        temp_double = temp_double + double.Parse(lb_TongTien_inputNVL.Text);

                        lb_Tong_inputNVL.Text = string.Format("{0:###,###.##}", SoLuong_d);
                        //ThanhTien_d = ThanhTien_d + double.Parse(lb_TongTien_inputNVL.Text);
                        lb_TongTien_inputNVL.Text = string.Format("{0:###,###,###}", temp_double);
                        cb_TenSP_inputNVL.Text = "";
                        txt_SoLuong_inputNVL.Text = "";
                        txt_DonGia_inputNVL.Text = "";
                        txt_GhiChu_inputNVL.Text = "";
                        cb_TenSP_inputNVL.Focus();
                        
                    }
                    

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }

        }
        private void rd_BaoBi_inputNVL_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rd_BaoBi_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Bold);
            rd_DauMau_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Regular);
            rd_Fitting_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Regular);
            /*rd_DauMau_inputNVL.Enabled = false;
            rd_Fitting_inputNVL.Enabled = false;*/
        }

        private void rd_Fitting_inputNVL_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rd_BaoBi_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Regular);
            rd_DauMau_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Regular);
            rd_Fitting_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Bold);
            /*rd_DauMau_inputNVL.Enabled = false;
            rd_BaoBi_inputNVL.Enabled = false;*/
        }

        private void rd_DauMau_inputNVL_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rd_BaoBi_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Regular);
            rd_DauMau_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Bold);
            rd_Fitting_inputNVL.Font = new Font(rd_BaoBi_inputNVL.Font, FontStyle.Regular);
            /*rd_BaoBi_inputNVL.Enabled = false;
            rd_Fitting_inputNVL.Enabled = false;*/
        }

        private void bt_insert_inputNVL_Click(object sender, EventArgs e)
        {
            add_grid_inputNVL();
        }

        private void radGridView12_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView12.Rows.Count > 0)
            {
                try
                {
                    //MessageBox.Show(radGridView12.Rows.Count.ToString());
                    int r = radGridView12.CurrentCell.RowIndex;

                    temp_double = double.Parse(lb_Tong_inputNVL.Text) - double.Parse(radGridView12.Rows[r].Cells["SL_inputNVL"].Value.ToString());
                    lb_Tong_inputNVL.Text = string.Format("{0:###,###.##}", temp_double);
                    temp_double = double.Parse(lb_TongTien_inputNVL.Text) - double.Parse(radGridView12.Rows[r].Cells["ThanhTien_inputNVL"].Value.ToString());
                    lb_TongTien_inputNVL.Text = string.Format("{0:###,###.##}", temp_double);
                    radGridView12.Rows.RemoveAt(r);
                    j = radGridView12.Rows.Count - 1;
                    //radGridView12.CurrentCell = radGridView12.Rows[j].Cells["MaCTinput"];

                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (radGridView12.Rows.Count == 0)
            {
                lb_Tong_inputNVL.Text = "0";
                lb_TongTien_inputNVL.Text = "0";
                rd_Fitting_inputNVL.Enabled = true;
                rd_BaoBi_inputNVL.Enabled = true;
                rd_DauMau_inputNVL.Enabled = true;
            }

        }
        private void reset_inputNVL()
        {
            txt_SoPhieu_inputNVL.Text = "";
            cb_LyDo_inputNVL.Text = "";
            cb_NoiNhan_inputNVL.Text = "";
            txt_DienGiai_inputNVL.Text = "";
            txt_SoLuong_inputNVL.Text = "";
            txt_DonGia_inputNVL.Text = "";
            radGridView12.Rows.Clear();
            lb_Serial_addinputNVL.Text = "0";
            bt_insert_inputNVL.Enabled = true;
            bt_save_inputNVL.Enabled = true;
            bt_edit_inputNVL.Enabled = false;
            bt_delete_inputNVL.Enabled = false;
            lb_TongTien_inputNVL.Text = "0";
            lb_Tong_inputNVL.Text = "0";
            lb_Serial_inputNVL.Text = "0";
            lb_Serial_addinputNVL.Visible = false;
            lb_Serial_DDinputNVL.Visible = false;
            txt_SoPhieu_inputNVL.Focus();

            
        }
        private void save_inputNVL()
        {
            SqlCommand cmd = new SqlCommand();
            
            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                if (radGridView12.Rows.Count > 0)
                {
                    LyDo1 = prs.substring_right(cb_LyDo_inputNVL.Text, child);
                    NoiGiao1 = prs.substring_left(cb_NoiGiao_inputNVL.Text, child);
                    NoiNhan1 = prs.substring_left(cb_NoiNhan_inputNVL.Text, child);
                    GhiChu1 = txt_DienGiai_inputNVL.Text;
                    string[] temp = dtp_Date_inputNVL.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    if (rd_BaoBi_inputNVL.IsChecked == true)
                        Nhom1 = "BaoBi";
                    if (rd_Fitting_inputNVL.IsChecked == true)
                        Nhom1 = "Fitting";
                    if (rd_DauMau_inputNVL.IsChecked == true)
                        Nhom1 = "DauMau";
                    if (LyDo1 == "" || NoiGiao1 == "" || NoiNhan1 == "")
                        MessageBox.Show("Nhấp sai thông tin, Vui lòng kiểm tra lại");
                    else
                    {
                        if (lb_Serial_addinputNVL.Visible == true && lb_Serial_addinputNVL.Text != "0")
                        {
                            Serial1 = lb_Serial_addinputNVL.Text.Trim();
                        }
                            
                        else
                        {
                            cmd.CommandText = "Ins_NVL_ChungTu";
                            cmd.Parameters.AddWithValue("@SoPhieu", txt_SoPhieu_inputNVL.Text.Trim());
                            cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                            cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                            cmd.Parameters.AddWithValue("@Nhom", Nhom1);
                            cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            cmd.Parameters.AddWithValue("@DienGiai", GhiChu1);
                            SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                            code.Direction = ParameterDirection.Output;
                            cmd.Parameters.Add(code);
                            SqlDataReader sdr = cmd.ExecuteReader();
                            Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                            sdr.Close();
                            cmd.Parameters.Clear();

                        }
                        cmd.CommandText = "Ins_NVL_NhapXuat";
                        //MessageBox.Show(Serial1);
                        for (i = radGridView12.Rows.Count - 1; i >= 0; i--)
                        {
                            MaSP1 = radGridView12.Rows[i].Cells["MaSP_inputNVL"].Value.ToString();
                            SoLuong_d = double.Parse(radGridView12.Rows[i].Cells["SL_inputNVL"].Value.ToString());
                            DonGia1 = radGridView12.Rows[i].Cells["DonGia_inputNVL"].Value.ToString();
                           
                            //ThanhTien1 = radGridView12.Rows[i].Cells["ThanhTien_inputNVL"].Value.ToString();
                            DVT1 = radGridView12.Rows[i].Cells["DVT_inputNVL"].Value.ToString();
                            //MessageBox.Show(i.ToString());
                            GhiChu1 = radGridView12.Rows[i].Cells["GhiChu_inputNVL"].Value.ToString();
                            //MessageBox.Show("aaaa");
                            cmd.Parameters.AddWithValue("@MaHang", MaSP1);
                            cmd.Parameters.AddWithValue("@ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@SLNhap", SoLuong_d);
                            cmd.Parameters.AddWithValue("@SLXuat", "0");
                            cmd.Parameters.AddWithValue("@DonGia", DonGia1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.Parameters.AddWithValue("@UserNhap", lb_Useraccount.Text);
                            cmd.ExecuteNonQuery();
                            radGridView12.Rows.RemoveAt(i);
                            cmd.Parameters.Clear();
                        }
                        MessageBox.Show("Dữ liệu được lưu thành công");
                        temp.Clone();
                        txt_SoPhieu_inputNVL.Text = "";
                        cb_LyDo_inputNVL.Text = "";
                        cb_NoiNhan_inputNVL.Text = "";
                        txt_DienGiai_inputNVL.Text = "";
                        txt_SoLuong_inputNVL.Text = "";
                        txt_DonGia_inputNVL.Text = "";
                        lb_Tong_inputNVL.Text = "0";
                        lb_TongTien_inputNVL.Text = "0";
                        txt_SoPhieu_inputNVL.Focus();

                    }
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                
            }

        }
        private void bt_save_inputNVL_Click(object sender, EventArgs e)
        {
            save_inputNVL();
        }
        private void edit_inputNVL()
        {
            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            Serial1 = lb_Serial_inputNVL.Text;
            cmd.CommandText = "Upd_NVL_NhapXuat";
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SERIAL "+Serial1+" KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            try
            {
                if (TL == DialogResult.Yes)
                {
                    if (txt_SoLuong_inputNVL.Text == "" || cb_TenSP_inputNVL.Text == "")
                        MessageBox.Show("Thiếu thông tin");
                    else
                    {
                        MaSP1 = prs.substring_left(cb_TenSP_inputNVL.Text, child);
                        if (txt_DonGia_inputNVL.Text == "")
                            DonGia1 = "0";
                        else
                            DonGia1 = (double.Parse(txt_DonGia_inputNVL.Text)).ToString();
                        SoLuong_d = double.Parse(txt_SoLuong_inputNVL.Text);
                        DVT1 = cb_DVT_inputNVL.Text;
                        GhiChu1 = txt_GhiChu_inputNVL.Text;

                        cmd.Parameters.AddWithValue("@MaHang", MaSP1);

                        cmd.Parameters.AddWithValue("@SLNhap", SoLuong_d);
                        cmd.Parameters.AddWithValue("@SLXuat", "0");
                        cmd.Parameters.AddWithValue("@DonGia", DonGia1);
                        cmd.Parameters.AddWithValue("@DVT", DVT1);
                        cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                        cmd.Parameters.AddWithValue("@UserSua", lb_Useraccount.Text);
                        cmd.Parameters.AddWithValue("Serial", Serial1);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("CHỈNH SỬA THÀNH CÔNG");
                        reset_inputNVL();
                    }
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void Search_inputNVL()
        {
            if (txt_SoPhieu_inputNVL.Text != "" || txt_Serial_inputNVL.Text != "")
            {
                DataSet ds = new DataSet();
                Conn = prs.Connect();
                Conn.Open();
                try
                {
                    string_temp = @"SELECT  NVL_NhapXuat.[Serial],dbo.NVL_ChungTu.SoPhieu,NVL_NhapXuat.ChungTu,dbo.NVL_ChungTu.NoiGiao,dbo.NVL_ChungTu.NoiNhan,dbo.NVL_ChungTu.Ngay,dbo.NVL_ChungTu.LyDo,dbo.NVL_ChungTu.DienGiai
                                  ,NVL_VatTu.[MaHang],TenHang,dbo.NVL_ChungTu.Nhom
                                  ,[SLNhap]
                                  ,[SLXuat]
                                  ,NVL_NhapXuat.[DonGia]
                                   ,SLNhap*NVL_NhapXuat.DonGia as ThanhTien
                                  ,NVL_NhapXuat.[DVT]
                                  ,[GhiChu]
                              FROM [dbo].[NVL_NhapXuat] inner join dbo.NVL_ChungTu on NVL_NhapXuat.ChungTu=NVL_ChungTu.Serial inner join dbo.NVL_VatTu on NVL_VatTu.MaHang=NVL_NhapXuat.MaHang where SLNhap<>0  ";
                    if (txt_Serial_inputNVL.Text != "")
                    {
                        string_temp += " and NVL_ChungTu.Serial=" + txt_Serial_inputNVL.Text;
                    }
                    if (txt_SoPhieu_inputNVL.Text != "")
                        string_temp += " and NVL_ChungTu.SoPhieu='" + txt_SoPhieu_inputNVL.Text + "'";
                    ds = prs.dts(string_temp, Conn, ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (radGridView12.Rows.Count > 0)
                            radGridView12.Rows.Clear();
                        SoLuong_d=0;
                        ThanhTien_d=0;
                        if (ds.Tables[0].Rows[0]["Nhom"].ToString() == "BaoBi")
                            rd_BaoBi_inputNVL.IsChecked = true;
                        if (ds.Tables[0].Rows[0]["Nhom"].ToString() == "DauMau")
                            rd_DauMau_inputNVL.IsChecked = true;
                        if (ds.Tables[0].Rows[0]["Nhom"].ToString() == "Fitting")
                            rd_Fitting_inputNVL.IsChecked = true;
                        txt_SoPhieu_inputNVL.Text = ds.Tables[0].Rows[0]["SoPhieu"].ToString();
                        cb_NoiGiao_inputNVL.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                        cb_NoiNhan_inputNVL.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                        cb_LyDo_inputNVL.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                        dtp_Date_inputNVL.Text = ds.Tables[0].Rows[0]["Ngay"].ToString();
                        txt_DienGiai_inputNVL.Text = ds.Tables[0].Rows[0]["DienGiai"].ToString();
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            radGridView12.Rows.Add(ds.Tables[0].Rows[i]["MaHang"].ToString(), ds.Tables[0].Rows[i]["TenHang"].ToString(), ds.Tables[0].Rows[i]["SLNhap"].ToString(), ds.Tables[0].Rows[i]["DonGia"].ToString(), ds.Tables[0].Rows[i]["ThanhTien"].ToString(), ds.Tables[0].Rows[i]["DVT"].ToString(), ds.Tables[0].Rows[i]["GhiChu"].ToString(),ds.Tables[0].Rows[i]["Serial"].ToString());
                            SoLuong_d += double.Parse(ds.Tables[0].Rows[i]["SLNhap"].ToString());
                            ThanhTien_d += double.Parse(ds.Tables[0].Rows[i]["ThanhTien"].ToString());

                        }
                        lb_Tong_inputNVL.Text = string.Format("{0:###,###.##}", SoLuong_d);
                        lb_TongTien_inputNVL.Text = string.Format("{0:###,###,###}", ThanhTien_d);
                        bt_edit_inputNVL.Enabled = true;
                        bt_delete_inputNVL.Enabled = true;
                        bt_insert_inputNVL.Enabled = false;
                        bt_save_inputNVL.Enabled = false;
                    }
                    else
                        MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra lại thông tin tìm kiếm");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    ds.Clear();
                }
            }
            else
                MessageBox.Show("Bạn cần nhập Số Serial hoặc số phiếu");


        }
        private void bt_search_inputNVL_Click(object sender, EventArgs e)
        {
            Search_inputNVL();
        }
        private void Display_inputNVL(int indexs)
        {
            if (bt_insert_inputNVL.Enabled == false)
            {
                cb_TenSP_inputNVL.Text = radGridView12.Rows[indexs].Cells["MaSP_inputNVL"].Value.ToString()+" - " + radGridView12.Rows[indexs].Cells["TenSP_inputNVL"].Value.ToString();
                txt_SoLuong_inputNVL.Text = radGridView12.Rows[indexs].Cells["SL_inputNVL"].Value.ToString();
                txt_DonGia_inputNVL.Text = radGridView12.Rows[indexs].Cells["DonGia_inputNVL"].Value.ToString();
                cb_DVT_inputNVL.Text = radGridView12.Rows[indexs].Cells["DVT_inputNVL"].Value.ToString();
                txt_GhiChu_inputNVL.Text = radGridView12.Rows[indexs].Cells["GhiChu_inputNVL"].Value.ToString();
                lb_Serial_inputNVL.Text = radGridView12.Rows[indexs].Cells["Serial_inputNVL"].Value.ToString();  
            }
        }
        private void radGridView12_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_insert_inputNVL.Enabled == false)
                {
                    int r = radGridView12.CurrentCell.RowIndex;
                    Display_inputNVL(r);
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void bt_reset_inputNVL_Click(object sender, EventArgs e)
        {
            reset_inputNVL();
        }
        private void bt_prev_inputNVL_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView12.Rows.Count > 0)
                {

                    i = int.Parse(lb_index_inputNVL.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_inputNVL(i);
                        lb_index_inputNVL.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void bt_next_inputNVL_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView12.Rows.Count > 0)
                {

                    i = int.Parse(lb_index_inputNVL.Text);
                    if (i < radGridView12.Rows.Count - 1)
                    {
                        i++;
                        Display_inputNVL(i);
                        lb_index_inputNVL.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void bt_edit_inputNVL_Click(object sender, EventArgs e)
        {
            edit_inputNVL();
        }
        private void delete_inputNVL()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                DialogResult TL;
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA ID: " + lb_Serial_inputNVL.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (TL == DialogResult.Yes)
                {

                    string_temp = @"DELETE FROM [dbo].[NVL_NhapXuat] WHERE Serial=" + lb_Serial_inputNVL.Text;
                    cmd.CommandText = string_temp;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("DỮ LIỆU ĐƯỢC XÓA THÀNH CÔNG");
                    reset_inputNVL();

                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("DỮ LIỆU XÓA KHÔNG THÀNH CÔNG. VUI LÒNG THỰC HIỆN LẠI " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void bt_delete_inputNVL_Click(object sender, EventArgs e)
        {
            delete_inputNVL();
        }
        private void radGridView12_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            try
            {
                int r = radGridView12.CurrentCell.RowIndex;
                radGridView12.Rows[r].Cells["ThanhTien_inputNVL"].Value = double.Parse(radGridView12.Rows[r].Cells["SL_inputNVL"].Value.ToString()) * double.Parse(radGridView12.Rows[r].Cells["DonGia_inputNVL"].Value.ToString());
                ThanhTien_d = 0;
                for (i = radGridView12.Rows.Count - 1; i >= 0; i--)
                {
                    ThanhTien_d += double.Parse(radGridView12.Rows[i].Cells["ThanhTien_inputNVL"].Value.ToString());
                }
                lb_TongTien_inputNVL.Text = string.Format("{0:###,###,###}", ThanhTien_d);
            }
            catch
            {
            }
        }
        private void print_inputNVL()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                //Ngay1 = Date_Split(dtp_Date_outputLS.Text);
                if (txt_SoPhieu_inputNVL.Text == "" && txt_Serial_inputNVL.Text == "")
                {
                    MessageBox.Show("Bạn cần nhập số Serial hoặc Số Phiếu để in chứng từ");

                }
                else
                {
                    string_temp = @" SELECT     dbo.NVL_VatTu.MaHang,NVL_VatTu.TenHang,NVL_NhapXuat.SLNhap as SoLuong,NVL_NhapXuat.DonGia,NVL_NhapXuat.DonGia*NVL_NhapXuat.SLNhap as ThanhTien,NVL_NhapXuat.GhiChu,NVL_ChungTu.SoPhieu as ChungTu,LyDo.LyDo,NVL_ChungTu.NoiGiao,NVL_ChungTu.NoiNhan,NVL_ChungTu.DienGiai,NVL_ChungTu.Ngay,NVL_NhapXuat.DVT,NVL_ChungTu.Serial
                FROM         dbo.NVL_NhapXuat INNER JOIN
                                      dbo.NVL_ChungTu ON dbo.NVL_NhapXuat.ChungTu = dbo.NVL_ChungTu.Serial INNER JOIN
                                      dbo.NVL_VatTu ON dbo.NVL_NhapXuat.MaHang = dbo.NVL_VatTu.MaHang left join LyDo on LyDo.MaLyDo=NVL_ChungTu.LyDo where SLNhap<>0";


                    if (txt_Serial_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.Serial=" + txt_Serial_inputNVL.Text;
                    if (txt_SoPhieu_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.SoPhieu='" + txt_SoPhieu_inputNVL.Text + "'";

                    //MessageBox.Show(string_temp);
                    ds = prs.dts(string_temp, Conn, ds);

                    Fr_Report FrR = new Fr_Report();

                    //FrR.report_KV1NKTG(ds, Ngay1);


                    FrR.report_inputNVL(ds, lb_HovaTen.Text);


                    FrR.Show();
                    //ds.Clear();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }
        
        }
        private void print_inputNVL_TH()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                //Ngay1 = Date_Split(dtp_Date_outputLS.Text);

                string_temp = @" SELECT     dbo.NVL_VatTu.MaHang,NVL_VatTu.TenHang,NVL_NhapXuat.SLNhap as SoLuong,NVL_NhapXuat.DonGia,NVL_NhapXuat.DonGia*NVL_NhapXuat.SLNhap as ThanhTien,NVL_NhapXuat.GhiChu,NVL_ChungTu.SoPhieu as ChungTu,LyDo.LyDo,NVL_ChungTu.NoiGiao,NVL_ChungTu.NoiNhan,NVL_ChungTu.DienGiai,NVL_ChungTu.Ngay,NVL_NhapXuat.DVT,NVL_NhapXuat.Serial
                FROM         dbo.NVL_NhapXuat INNER JOIN
                                      dbo.NVL_ChungTu ON dbo.NVL_NhapXuat.ChungTu = dbo.NVL_ChungTu.Serial INNER JOIN
                                      dbo.NVL_VatTu ON dbo.NVL_NhapXuat.MaHang = dbo.NVL_VatTu.MaHang left join LyDo on LyDo.MaLyDo=NVL_ChungTu.LyDo where SLNhap<>0";


                    if (txt_Serial_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.Serial=" + txt_Serial_inputNVL.Text;
                    if (txt_SoPhieu_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.SoPhieu='" + txt_SoPhieu_inputNVL.Text + "'";
                    if (cb_NoiGiao_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.NoiGiao='" + prs.substring_left(cb_NoiGiao_inputNVL.Text, child)+"'";
                    if (cb_NoiNhan_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.NoiNhan='" + prs.substring_left(cb_NoiNhan_inputNVL.Text, child)+"'";
                    if (cb_LyDo_inputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.LyDo='" + prs.substring_right(cb_LyDo_inputNVL.Text, child)+"'";
                    if (cb_TenSP_inputNVL.Text != "")
                        string_temp += " and NVL_NhapXuat.MaHang='" + prs.substring_left(cb_TenSP_inputNVL.Text, child)+"'";
                    string[] temp = dtp_Date_inputNVL.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    string_temp += " and dbo.NVL_ChungTu.Ngay='" + Ngay1 + "'";
                    //MessageBox.Show(string_temp);
                    ds = prs.dts(string_temp, Conn, ds);

                    Fr_Report FrR = new Fr_Report();

                    //FrR.report_KV1NKTG(ds, Ngay1);


                    FrR.report_inputNVLTH(ds, lb_HovaTen.Text);


                    FrR.Show();
                    //ds.Clear();
                    temp.Clone();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
                
            }

        }
        private void bt_print_inputNVL_Click(object sender, EventArgs e)
        {
            print_inputNVL();
        }
       
        #endregion
        #region //out put NVL
        private void txt_SoLuong_outputNVL_Leave(object sender, EventArgs e)
        {
            try
            {
                if (txt_SoLuong_outputNVL.Text != "")
                {
                    if (double.Parse(txt_SoLuong_outputNVL.Text) > double.Parse(lb_TonKho_outputNVL.Text))
                    {
                        MessageBox.Show("SL Xuất không vượt quá số lượng TỒN KHO");
                        txt_SoLuong_outputNVL.Text = "";
                        txt_SoLuong_outputNVL.Focus();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Dữ liệu nhập phải là số",ex.Message);
            }
            //prs.checktext_number(txt_SoLuong_outputNVL);

        }
        private void add_grid_outputNVL()
        {
            try
            {
                if (cb_TenSP_outputNVL.Text == "" || cb_LyDo_outputNVL.Text == "" || cb_NoiGiao_outputNVL.Text == "" || cb_NoiNhan_outputNVL.Text == "" || txt_SoLuong_outputNVL.Text == "" || txt_SoPhieu_outputNVL.Text == "")
                    MessageBox.Show("Nhập thiếu thông tin, vui lòng kiểm tra lại");
                else
                {

                    MaSP1 = prs.substring_left(cb_TenSP_outputNVL.Text, child);
                    TenSP1 = prs.substring_right(cb_TenSP_outputNVL.Text, child);

                    if (MaSP1 == "" || TenSP1 == "")
                        MessageBox.Show("Nhập sai thông tin, vui lòng kiểm tra lại");
                    else
                    {

                        
                        SoLuong_d = 0;
                        temp_double = 0;
                        count = 0;
                        SLXuat_d = double.Parse(txt_SoLuong_outputNVL.Text);
                        //DVT1 = cb_DVT_outputNVL.Text.Trim();
                        GhiChu1 = txt_GhiChu_outputNVL.Text.Trim();
                        if (rd_BaoBi_outputNVL.IsChecked == true)
                            Nhom1 = "BaoBi";
                        if (rd_DauMau_outputNVL.IsChecked == true)
                            Nhom1 = "DauMau";
                        if (rd_Fitting_outputNVL.IsChecked == true)
                            Nhom1 = "Fitting";
                        for (i = dataGridView1.Rows.Count - 2; i >= 0; i--)
                        {
                            string_temp = dataGridView1.Rows[i].Cells["MaSP"].Value.ToString();
                            PhanLoai1 = dataGridView1.Rows[i].Cells["Nhom"].Value.ToString().Trim();
                            // MessageBox.Show(string_temp + " " + Nhom1);
                            if (MaSP1 == string_temp && PhanLoai1 == Nhom1)
                            {
                                string_temp = dataGridView1.Rows[i].Cells["MaChiTiet"].Value.ToString();
                                TenSP1 = dataGridView1.Rows[i].Cells["TenHang"].Value.ToString();
                                SoLuong1 = (double.Parse(dataGridView1.Rows[i].Cells["SLQuyDoi"].Value.ToString()) * SLXuat_d).ToString();
                                DonGia1 = dataGridView1.Rows[i].Cells["DonGia"].Value.ToString();
                                DVT1 = dataGridView1.Rows[i].Cells["DVT"].Value.ToString();
                                ThanhTien_d = double.Parse(DonGia1) * double.Parse(SoLuong1);
                                radGridView13.Rows.Add(string_temp, TenSP1, SoLuong1, DonGia1, ThanhTien_d, DVT1, GhiChu1);
                                SoLuong_d += double.Parse(SoLuong1);
                                temp_double += ThanhTien_d;
                                count++;
                            }
                        }
                        if (count == 0)
                        {
                            if (txt_DonGia_outputNVL.Text == "")
                                DonGia1 = "0";
                            else
                                DonGia1 = txt_DonGia_outputNVL.Text.Trim();
                            SoLuong_d = double.Parse(txt_SoLuong_outputNVL.Text);
                            ThanhTien_d = double.Parse(DonGia1) * double.Parse(txt_SoLuong_outputNVL.Text);
                            temp_double += ThanhTien_d;
                            DVT1 = cb_DVT_outputNVL.Text;
                            radGridView13.Rows.Add(MaSP1, TenSP1, txt_SoLuong_outputNVL.Text, DonGia1, ThanhTien_d, DVT1, GhiChu1);
                        }

                        SoLuong_d = SoLuong_d + double.Parse(lb_Tong_outputNVL.Text);
                        temp_double = temp_double + double.Parse(lb_TongTien_outputNVL.Text);

                        lb_Tong_outputNVL.Text = string.Format("{0:###,###.##}", SoLuong_d);
                        //ThanhTien_d = ThanhTien_d + double.Parse(lb_TongTien_outputNVL.Text);
                        lb_TongTien_outputNVL.Text = string.Format("{0:###,###,###}", temp_double);
                        cb_TenSP_outputNVL.Text = "";
                        txt_SoLuong_outputNVL.Text = "";
                        txt_DonGia_outputNVL.Text = "";
                        txt_GhiChu_outputNVL.Text = "";
                        cb_TenSP_outputNVL.Focus();

                    }


                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }

        }
        private void rd_BaoBi_outputNVL_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rd_BaoBi_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Bold);
            rd_DauMau_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Regular);
            rd_Fitting_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Regular);
            /*rd_DauMau_outputNVL.Enabled = false;
            rd_Fitting_outputNVL.Enabled = false;*/
        }

        private void rd_Fitting_outputNVL_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rd_BaoBi_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Regular);
            rd_DauMau_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Regular);
            rd_Fitting_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Bold);
            /*rd_DauMau_outputNVL.Enabled = false;
            rd_BaoBi_outputNVL.Enabled = false;*/
        }

        private void rd_DauMau_outputNVL_ToggleStateChanged(object sender, StateChangedEventArgs args)
        {
            rd_BaoBi_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Regular);
            rd_DauMau_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Bold);
            rd_Fitting_outputNVL.Font = new Font(rd_BaoBi_outputNVL.Font, FontStyle.Regular);
            /*rd_BaoBi_outputNVL.Enabled = false;
            rd_Fitting_outputNVL.Enabled = false;*/
        }

        private void bt_insert_outputNVL_Click(object sender, EventArgs e)
        {
            add_grid_outputNVL();
        }

        private void radGridView13_DoubleClick(object sender, EventArgs e)
        {
            if (radGridView13.Rows.Count > 0)
            {
                try
                {
                    //MessageBox.Show(radGridView13.Rows.Count.ToString());
                    int r = radGridView13.CurrentCell.RowIndex;

                    temp_double = double.Parse(lb_Tong_outputNVL.Text) - double.Parse(radGridView13.Rows[r].Cells["SL_outputNVL"].Value.ToString());
                    lb_Tong_outputNVL.Text = string.Format("{0:###,###.##}", temp_double);
                    temp_double = double.Parse(lb_TongTien_outputNVL.Text) - double.Parse(radGridView13.Rows[r].Cells["ThanhTien_outputNVL"].Value.ToString());
                    lb_TongTien_outputNVL.Text = string.Format("{0:###,###.##}", temp_double);
                    radGridView13.Rows.RemoveAt(r);
                    j = radGridView13.Rows.Count - 1;
                    //radGridView13.CurrentCell = radGridView13.Rows[j].Cells["MaCTinput"];

                }
                catch (Exception ex)
                { MessageBox.Show(ex.Message); }
            }
            if (radGridView13.Rows.Count == 0)
            {
                lb_Tong_outputNVL.Text = "0";
                lb_TongTien_outputNVL.Text = "0";
                rd_Fitting_outputNVL.Enabled = true;
                rd_BaoBi_outputNVL.Enabled = true;
                rd_DauMau_outputNVL.Enabled = true;
            }

        }
        private void reset_outputNVL()
        {
            txt_SoPhieu_outputNVL.Text = "";
            cb_LyDo_outputNVL.Text = "";
            cb_NoiNhan_outputNVL.Text = "";
            txt_DienGiai_outputNVL.Text = "";
            txt_SoLuong_outputNVL.Text = "";
            txt_DonGia_outputNVL.Text = "";
            radGridView13.Rows.Clear();
            lb_Serial_addoutputNVL.Text = "0";
            bt_insert_outputNVL.Enabled = true;
            bt_save_outputNVL.Enabled = true;
            bt_edit_outputNVL.Enabled = false;
            bt_delete_outputNVL.Enabled = false;
            lb_TongTien_outputNVL.Text = "0";
            lb_Tong_outputNVL.Text = "0";
            lb_Serial_outputNVL.Text = "0";
            lb_Serial_addoutputNVL.Visible = false;
            lb_Serial_DDoutputNVL.Visible = false;
            txt_SoPhieu_outputNVL.Focus();


        }
        private void save_outputNVL()
        {
            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                if (radGridView13.Rows.Count > 0)
                {
                    LyDo1 = prs.substring_right(cb_LyDo_outputNVL.Text, child);
                    NoiGiao1 = prs.substring_left(cb_NoiGiao_outputNVL.Text, child);
                    NoiNhan1 = prs.substring_left(cb_NoiNhan_outputNVL.Text, child);
                    GhiChu1 = txt_DienGiai_outputNVL.Text;
                    string[] temp = dtp_Date_outputNVL.Text.Split('/');
                    Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                    if (rd_BaoBi_outputNVL.IsChecked == true)
                        Nhom1 = "BaoBi";
                    if (rd_Fitting_outputNVL.IsChecked == true)
                        Nhom1 = "Fitting";
                    if (rd_DauMau_outputNVL.IsChecked == true)
                        Nhom1 = "DauMau";
                    if (LyDo1 == "" || NoiGiao1 == "" || NoiNhan1 == "")
                        MessageBox.Show("Nhấp sai thông tin, Vui lòng kiểm tra lại");
                    else
                    {
                        if (lb_Serial_addoutputNVL.Visible == true && lb_Serial_addoutputNVL.Text != "0")
                        {
                            Serial1 = lb_Serial_addoutputNVL.Text.Trim();
                        }

                        else
                        {
                            cmd.CommandText = "Ins_NVL_ChungTu";
                            cmd.Parameters.AddWithValue("@SoPhieu", txt_SoPhieu_outputNVL.Text.Trim());
                            cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                            cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                            cmd.Parameters.AddWithValue("@Nhom", Nhom1);
                            cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            cmd.Parameters.AddWithValue("@DienGiai", GhiChu1);
                            SqlParameter code = new SqlParameter("@Serial", SqlDbType.Int);
                            code.Direction = ParameterDirection.Output;
                            cmd.Parameters.Add(code);
                            SqlDataReader sdr = cmd.ExecuteReader();
                            Serial1 = cmd.Parameters["@Serial"].Value.ToString();
                            sdr.Close();
                            cmd.Parameters.Clear();

                        }
                        cmd.CommandText = "Ins_NVL_NhapXuat";
                        //MessageBox.Show(Serial1);
                        for (i = radGridView13.Rows.Count - 1; i >= 0; i--)
                        {
                            MaSP1 = radGridView13.Rows[i].Cells["MaSP_outputNVL"].Value.ToString();
                            SoLuong_d = double.Parse(radGridView13.Rows[i].Cells["SL_outputNVL"].Value.ToString());
                            DonGia1 = radGridView13.Rows[i].Cells["DonGia_outputNVL"].Value.ToString();

                            //ThanhTien1 = radGridView13.Rows[i].Cells["ThanhTien_outputNVL"].Value.ToString();
                            DVT1 = radGridView13.Rows[i].Cells["DVT_outputNVL"].Value.ToString();
                            //MessageBox.Show(i.ToString());
                            GhiChu1 = radGridView13.Rows[i].Cells["GhiChu_outputNVL"].Value.ToString();
                            //MessageBox.Show("aaaa");
                            cmd.Parameters.AddWithValue("@MaHang", MaSP1);
                            cmd.Parameters.AddWithValue("@ChungTu", Serial1);
                            cmd.Parameters.AddWithValue("@SLNhap", "0");
                            cmd.Parameters.AddWithValue("@SLXuat", SoLuong_d);
                            cmd.Parameters.AddWithValue("@DonGia", DonGia1);
                            cmd.Parameters.AddWithValue("@DVT", DVT1);
                            cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                            cmd.Parameters.AddWithValue("@UserNhap", lb_Useraccount.Text);
                            cmd.ExecuteNonQuery();
                            radGridView13.Rows.RemoveAt(i);
                            cmd.Parameters.Clear();
                        }
                        MessageBox.Show("Dữ liệu được lưu thành công");
                        temp.Clone();
                        txt_SoPhieu_outputNVL.Text = "";
                        cb_LyDo_outputNVL.Text = "";
                        cb_NoiNhan_outputNVL.Text = "";
                        txt_DienGiai_outputNVL.Text = "";
                        txt_SoLuong_outputNVL.Text = "";
                        txt_DonGia_outputNVL.Text = "";
                        lb_Tong_outputNVL.Text = "0";
                        lb_TongTien_outputNVL.Text = "0";
                        txt_SoPhieu_outputNVL.Focus();

                    }
                }
            }
            catch (Exception ex)
            {
                string s = "insert duplicate key";
                string FK = "FOREIGN KEY constraint";
                string s1 = ex.Message.ToString();
                if (s1.Contains(s))
                { MessageBox.Show("Insert không thành công \n" + "Mã đã tồn tại "); }
                else if (s1.Contains(FK))
                { MessageBox.Show("Nhập sai thông tin trong Combobox"); }
                else
                    MessageBox.Show(ex.Message);
                DialogResult Err;
                Err = MessageBox.Show("Đã xảy ra lỗi, dữ liệu lưu không thành công " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();

            }

        }
        private void bt_save_outputNVL_Click(object sender, EventArgs e)
        {
            save_outputNVL();
        }
        private void edit_outputNVL()
        {
            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.StoredProcedure;
            Conn = prs.Connect();
            Conn.Open();
            cmd.Connection = Conn;
            Serial1 = lb_Serial_outputNVL.Text;
            cmd.CommandText = "Upd_NVL_NhapXuat";
            DialogResult TL;
            TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SERIAL " + Serial1 + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            try
            {
                if (TL == DialogResult.Yes)
                {
                    if (txt_SoLuong_outputNVL.Text == "" || cb_TenSP_outputNVL.Text == "")
                        MessageBox.Show("Thiếu thông tin");
                    else
                    {
                        MaSP1 = prs.substring_left(cb_TenSP_outputNVL.Text, child);
                        if (txt_DonGia_outputNVL.Text == "")
                            DonGia1 = "0";
                        else
                            DonGia1 = (double.Parse(txt_DonGia_outputNVL.Text)).ToString();
                        SoLuong_d = double.Parse(txt_SoLuong_outputNVL.Text);
                        DVT1 = cb_DVT_outputNVL.Text;
                        GhiChu1 = txt_GhiChu_outputNVL.Text;

                        cmd.Parameters.AddWithValue("@MaHang", MaSP1);

                        cmd.Parameters.AddWithValue("@SLNhap", "0");
                        cmd.Parameters.AddWithValue("@SLXuat", SoLuong_d);
                        cmd.Parameters.AddWithValue("@DonGia", DonGia1);
                        cmd.Parameters.AddWithValue("@DVT", DVT1);
                        cmd.Parameters.AddWithValue("@GhiChu", GhiChu1);
                        cmd.Parameters.AddWithValue("@UserSua", lb_Useraccount.Text);
                        cmd.Parameters.AddWithValue("@Serial", Serial1);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("CHỈNH SỬA THÀNH CÔNG");
                        
                        reset_outputNVL();
                    }
                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("CHỈNH SỬA KHÔNG THÀNH CÔNG " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void Search_outputNVL()
        {
            if (txt_SoPhieu_outputNVL.Text != "" || txt_Serial_outputNVL.Text != "")
            {
                DataSet ds = new DataSet();
                Conn = prs.Connect();
                Conn.Open();
                try
                {
                    string_temp = @"SELECT  NVL_NhapXuat.[Serial],dbo.NVL_ChungTu.SoPhieu,NVL_NhapXuat.ChungTu,dbo.NVL_ChungTu.NoiGiao,dbo.NVL_ChungTu.NoiNhan,dbo.NVL_ChungTu.Ngay,dbo.NVL_ChungTu.LyDo,dbo.NVL_ChungTu.DienGiai
                                  ,NVL_VatTu.[MaHang],TenHang,dbo.NVL_ChungTu.Nhom
                                  ,[SLNhap]
                                  ,[SLXuat]
                                  ,NVL_NhapXuat.[DonGia]
                                   ,SLXuat*NVL_NhapXuat.DonGia as ThanhTien
                                  ,NVL_NhapXuat.[DVT]
                                  ,[GhiChu]
                              FROM [dbo].[NVL_NhapXuat] inner join dbo.NVL_ChungTu on NVL_NhapXuat.ChungTu=NVL_ChungTu.Serial inner join dbo.NVL_VatTu on NVL_VatTu.MaHang=NVL_NhapXuat.MaHang where SLXuat<>0  ";
                    if (txt_Serial_outputNVL.Text != "")
                    {
                        string_temp += " and NVL_ChungTu.Serial=" + txt_Serial_outputNVL.Text;
                    }
                    if (txt_SoPhieu_outputNVL.Text != "")
                        string_temp += " and NVL_ChungTu.SoPhieu='" + txt_SoPhieu_outputNVL.Text + "'";
                    ds = prs.dts(string_temp, Conn, ds);
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        if (radGridView13.Rows.Count > 0)
                            radGridView13.Rows.Clear();
                        SoLuong_d = 0;
                        ThanhTien_d = 0;
                        if (ds.Tables[0].Rows[0]["Nhom"].ToString() == "BaoBi")
                            rd_BaoBi_outputNVL.IsChecked = true;
                        if (ds.Tables[0].Rows[0]["Nhom"].ToString() == "DauMau")
                            rd_DauMau_outputNVL.IsChecked = true;
                        if (ds.Tables[0].Rows[0]["Nhom"].ToString() == "Fitting")
                            rd_Fitting_outputNVL.IsChecked = true;
                        txt_SoPhieu_outputNVL.Text = ds.Tables[0].Rows[0]["SoPhieu"].ToString();
                        cb_NoiGiao_outputNVL.Text = ds.Tables[0].Rows[0]["NoiGiao"].ToString();
                        cb_NoiNhan_outputNVL.Text = ds.Tables[0].Rows[0]["NoiNhan"].ToString();
                        cb_LyDo_outputNVL.Text = ds.Tables[0].Rows[0]["LyDo"].ToString();
                        dtp_Date_outputNVL.Text = ds.Tables[0].Rows[0]["Ngay"].ToString();
                        txt_DienGiai_outputNVL.Text = ds.Tables[0].Rows[0]["DienGiai"].ToString();
                        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                        {
                            radGridView13.Rows.Add(ds.Tables[0].Rows[i]["MaHang"].ToString(), ds.Tables[0].Rows[i]["TenHang"].ToString(), ds.Tables[0].Rows[i]["SLXuat"].ToString(), ds.Tables[0].Rows[i]["DonGia"].ToString(), ds.Tables[0].Rows[i]["ThanhTien"].ToString(), ds.Tables[0].Rows[i]["DVT"].ToString(), ds.Tables[0].Rows[i]["GhiChu"].ToString(), ds.Tables[0].Rows[i]["Serial"].ToString());
                            SoLuong_d += double.Parse(ds.Tables[0].Rows[i]["SLXuat"].ToString());
                            ThanhTien_d += double.Parse(ds.Tables[0].Rows[i]["ThanhTien"].ToString());

                        }
                        lb_Tong_outputNVL.Text = string.Format("{0:###,###.##}", SoLuong_d);
                        lb_TongTien_outputNVL.Text = string.Format("{0:###,###,###}", ThanhTien_d);
                        bt_edit_outputNVL.Enabled = true;
                        bt_delete_outputNVL.Enabled = true;
                        bt_insert_outputNVL.Enabled = false;
                        bt_save_outputNVL.Enabled = false;
                    }
                    else
                        MessageBox.Show("Không có dữ liệu, vui lòng kiểm tra lại thông tin tìm kiếm");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    ds.Clear();
                }
            }
            else
                MessageBox.Show("Bạn cần nhập Số Serial hoặc số phiếu");


        }
        private void bt_search_outputNVL_Click(object sender, EventArgs e)
        {
            Search_outputNVL();
        }
        private void Display_outputNVL(int indexs)
        {
            if (bt_insert_outputNVL.Enabled == false)
            {
                cb_TenSP_outputNVL.Text = radGridView13.Rows[indexs].Cells["MaSP_outputNVL"].Value.ToString() + " - " + radGridView13.Rows[indexs].Cells["TenSP_outputNVL"].Value.ToString();
                txt_SoLuong_outputNVL.Text = radGridView13.Rows[indexs].Cells["SL_outputNVL"].Value.ToString();
                txt_DonGia_outputNVL.Text = radGridView13.Rows[indexs].Cells["DonGia_outputNVL"].Value.ToString();
                cb_DVT_outputNVL.Text = radGridView13.Rows[indexs].Cells["DVT_outputNVL"].Value.ToString();
                txt_GhiChu_outputNVL.Text = radGridView13.Rows[indexs].Cells["GhiChu_outputNVL"].Value.ToString();
                lb_Serial_outputNVL.Text = radGridView13.Rows[indexs].Cells["Serial_outputNVL"].Value.ToString();
            }
        }
        private void radGridView13_Click(object sender, EventArgs e)
        {
            try
            {
                if (bt_insert_outputNVL.Enabled == false)
                {
                    int r = radGridView13.CurrentCell.RowIndex;
                    Display_outputNVL(r);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void bt_reset_outputNVL_Click(object sender, EventArgs e)
        {
            reset_outputNVL();
        }
        private void bt_prev_outputNVL_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView13.Rows.Count > 0)
                {

                    i = int.Parse(lb_index_outputNVL.Text);
                    if (i > 0)
                    {
                        i--;
                        Display_outputNVL(i);
                        lb_index_outputNVL.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void bt_next_outputNVL_Click(object sender, EventArgs e)
        {
            try
            {
                if (radGridView13.Rows.Count > 0)
                {

                    i = int.Parse(lb_index_outputNVL.Text);
                    if (i < radGridView13.Rows.Count - 1)
                    {
                        i++;
                        Display_outputNVL(i);
                        lb_index_outputNVL.Text = i.ToString();
                    }
                }
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }
        private void bt_edit_outputNVL_Click(object sender, EventArgs e)
        {
            edit_outputNVL();
        }
        private void delete_outputNVL()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            try
            {
                DialogResult TL;
                TL = MessageBox.Show("BẠN CÓ MUỐN XÓA ID: " + lb_Serial_outputNVL.Text + " KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (TL == DialogResult.Yes)
                {

                    string_temp = @"DELETE FROM [dbo].[NVL_NhapXuat] WHERE Serial=" + lb_Serial_outputNVL.Text;
                    cmd.CommandText = string_temp;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("DỮ LIỆU ĐƯỢC XÓA THÀNH CÔNG");
                    reset_outputNVL();

                }
            }
            catch (Exception ex)
            {
                DialogResult Err;
                Err = MessageBox.Show("DỮ LIỆU XÓA KHÔNG THÀNH CÔNG. VUI LÒNG THỰC HIỆN LẠI " + ex.ToString(), "THÔNG BÁO", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
            }
        }
        private void radGridView13_CellEndEdit(object sender, GridViewCellEventArgs e)
        {
            try
            {
                int r = radGridView13.CurrentCell.RowIndex;
                radGridView13.Rows[r].Cells["ThanhTien_outputNVL"].Value = double.Parse(radGridView13.Rows[r].Cells["SL_outputNVL"].Value.ToString()) * double.Parse(radGridView13.Rows[r].Cells["DonGia_outputNVL"].Value.ToString());
                ThanhTien_d = 0;
                for (i = radGridView13.Rows.Count - 1; i >= 0; i--)
                {
                    ThanhTien_d += double.Parse(radGridView13.Rows[i].Cells["ThanhTien_outputNVL"].Value.ToString());
                }
                lb_TongTien_outputNVL.Text = string.Format("{0:###,###,###}", ThanhTien_d);
            }
            catch
            {
            }
        }
        private void print_outputNVL()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                //Ngay1 = Date_Split(dtp_Date_outputLS.Text);
                if (txt_SoPhieu_outputNVL.Text == "" && txt_Serial_outputNVL.Text == "")
                {
                    MessageBox.Show("Bạn cần nhập số Serial hoặc Số Phiếu để in chứng từ");

                }
                else
                {
                    string_temp = @" SELECT     dbo.NVL_VatTu.MaHang,NVL_VatTu.TenHang,NVL_NhapXuat.SLXuat as SoLuong,NVL_NhapXuat.DonGia,NVL_NhapXuat.DonGia*NVL_NhapXuat.SLXuat as ThanhTien,NVL_NhapXuat.GhiChu,NVL_ChungTu.SoPhieu as ChungTu,LyDo.LyDo,NVL_ChungTu.NoiGiao,NVL_ChungTu.NoiNhan,NVL_ChungTu.DienGiai,NVL_ChungTu.Ngay,NVL_NhapXuat.DVT,NVL_ChungTu.Serial
                FROM         dbo.NVL_NhapXuat INNER JOIN
                                      dbo.NVL_ChungTu ON dbo.NVL_NhapXuat.ChungTu = dbo.NVL_ChungTu.Serial INNER JOIN
                                      dbo.NVL_VatTu ON dbo.NVL_NhapXuat.MaHang = dbo.NVL_VatTu.MaHang left join LyDo on LyDo.MaLyDo=NVL_ChungTu.LyDo where SLXuat<>0";


                    if (txt_Serial_outputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.Serial=" + txt_Serial_outputNVL.Text;
                    if (txt_SoPhieu_outputNVL.Text != "")
                        string_temp += " and dbo.NVL_ChungTu.SoPhieu='" + txt_SoPhieu_outputNVL.Text + "'";

                    //MessageBox.Show(string_temp);
                    ds = prs.dts(string_temp, Conn, ds);

                    Fr_Report FrR = new Fr_Report();

                    //FrR.report_KV1NKTG(ds, Ngay1);


                    FrR.report_outputNVL(ds, lb_HovaTen.Text);


                    FrR.Show();
                    //ds.Clear();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();
            }

        }
        private void print_outputNVL_TH()
        {
            Conn = prs.Connect();
            Conn.Open();
            GhiChu1 = "";
            DataSet ds = new DataSet();
            try
            {
                //Ngay1 = Date_Split(dtp_Date_outputLS.Text);

                string_temp = @" SELECT     dbo.NVL_VatTu.MaHang,NVL_VatTu.TenHang,NVL_NhapXuat.SLXuat as SoLuong,NVL_NhapXuat.DonGia,NVL_NhapXuat.DonGia*NVL_NhapXuat.SLXuat as ThanhTien,NVL_NhapXuat.GhiChu,NVL_ChungTu.SoPhieu as ChungTu,LyDo.LyDo,NVL_ChungTu.NoiGiao,NVL_ChungTu.NoiNhan,NVL_ChungTu.DienGiai,NVL_ChungTu.Ngay,NVL_NhapXuat.DVT,NVL_NhapXuat.Serial
                FROM         dbo.NVL_NhapXuat INNER JOIN
                                      dbo.NVL_ChungTu ON dbo.NVL_NhapXuat.ChungTu = dbo.NVL_ChungTu.Serial INNER JOIN
                                      dbo.NVL_VatTu ON dbo.NVL_NhapXuat.MaHang = dbo.NVL_VatTu.MaHang left join LyDo on LyDo.MaLyDo=NVL_ChungTu.LyDo where SLXuat<>0";


                if (txt_Serial_outputNVL.Text != "")
                    string_temp += " and dbo.NVL_ChungTu.Serial=" + txt_Serial_outputNVL.Text;
                if (txt_SoPhieu_outputNVL.Text != "")
                    string_temp += " and dbo.NVL_ChungTu.SoPhieu='" + txt_SoPhieu_outputNVL.Text + "'";
                if (cb_NoiGiao_outputNVL.Text != "")
                    string_temp += " and dbo.NVL_ChungTu.NoiGiao='" + prs.substring_left(cb_NoiGiao_outputNVL.Text, child) + "'";
                if (cb_NoiNhan_outputNVL.Text != "")
                    string_temp += " and dbo.NVL_ChungTu.NoiNhan='" + prs.substring_left(cb_NoiNhan_outputNVL.Text, child) + "'";
                if (cb_LyDo_outputNVL.Text != "")
                    string_temp += " and dbo.NVL_ChungTu.LyDo='" + prs.substring_right(cb_LyDo_outputNVL.Text, child) + "'";
                if (cb_TenSP_outputNVL.Text != "")
                    string_temp += " and NVL_NhapXuat.MaHang='" + prs.substring_left(cb_TenSP_outputNVL.Text, child) + "'";
                string[] temp = dtp_Date_outputNVL.Text.Split('/');
                Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                string_temp += " and dbo.NVL_ChungTu.Ngay='" + Ngay1 + "'";
                //MessageBox.Show(string_temp);
                ds = prs.dts(string_temp, Conn, ds);

                Fr_Report FrR = new Fr_Report();

                //FrR.report_KV1NKTG(ds, Ngay1);


                FrR.report_outputNVLTH(ds, lb_HovaTen.Text);


                FrR.Show();
                //ds.Clear();
                temp.Clone();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                ds.Clear();

            }

        }
        private void bt_print_outputNVL_Click(object sender, EventArgs e)
        {
            print_outputNVL();
        }
        private void bt_delete_outputNVL_Click(object sender, EventArgs e)
        {
            delete_outputNVL();
        }
        private void cb_TenSP_outputNVL_Leave(object sender, EventArgs e)
        {
            if (cb_TenSP_outputNVL.Text != "")
            {
                SqlCommand cmd = new SqlCommand();

                Conn = prs.Connect();
                Conn.Open();
                cmd.Connection = Conn;
                cmd.CommandText = "Select MaSP,MaChiTiet,SLTon,SLTon/SLQuyDoi as DBTon,Nhom from dbo.NVL_TK";
                count = 0;
                temp_double = 1000000000;
                try
                {
                    MaSP1 = prs.substring_left(cb_TenSP_outputNVL.Text, child);
                    SqlDataReader rd = cmd.ExecuteReader();
                    if (rd_BaoBi_outputNVL.IsChecked == true)
                        Nhom1 = "BaoBi";
                    if (rd_Fitting_outputNVL.IsChecked == true)
                        Nhom1 = "Fitting";
                    if (rd_DauMau_outputNVL.IsChecked == true)
                        Nhom1 = "DauMau";
                    while (rd.Read())
                    {
                        if (MaSP1 == rd["MaSP"].ToString()&&Nhom1==rd["Nhom"].ToString())
                        {
                            if (temp_double > double.Parse(rd["DBTon"].ToString()))
                                temp_double = double.Parse(rd["DBTon"].ToString());
                            count++;
                        }

                    }

                    rd.Close();
                    if (count == 0)
                    {
                        //MessageBox.Show(count.ToString());
                        rd = cmd.ExecuteReader();
                        while (rd.Read())
                        {
                           // MessageBox.Show(rd["MaChiTiet"].ToString());
                            if (MaSP1 == rd["MaChiTiet"].ToString()&&Nhom1==rd["Nhom"].ToString())
                            {
                                
                                temp_double = double.Parse(rd["SLTon"].ToString());
                            }
                        }
                    }
                    if (temp_double > 0)
                        lb_TonKho_outputNVL.Text = string.Format("{0:###,###,###.##}", temp_double);
                    else
                        lb_TonKho_outputNVL.Text = temp_double.ToString();
                    rd.Dispose();

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    Conn.Close();
                    cmd.Dispose();
                    //rd.Dispose();
                }
            }
        }
        #endregion
        #region //Ton Kho
        private void TonKho_NVL()
        {

            DataSet ds = new DataSet();
            Conn = prs.Connect();
            Conn.Open();
            try
            {
                if (rd_BaoBi_TKNVL.IsChecked == true)
                        Nhom1 = "BaoBi";
                if (rd_Fitting_TKNVL.IsChecked == true)
                    Nhom1 = "Fitting";
                if (rd_DauMau_TKNVL.IsChecked == true)
                    Nhom1 = "DauMau";
                if (cb_TenSP_TKNVL.Text != "")
                {
                    MaSP1 = prs.substring_left(cb_TenSP_TKNVL.Text, child);
                    string_temp = @"select NVL_VatTu.TenHang as TenSP,qry1.* from
                            (select NVL_TK.MaSP,NVL_VatTu.TenHang as TenChiTiet, NVL_TK.MaChiTiet,NVL_TK.SLTon,NVL_TK.SLTon/NVL_TK.SLQuyDoi as DBTon, isnull(NVL_TK.ThanhTien,0) as ThanhTien
                            from dbo.NVL_VatTu inner join dbo.NVL_TK on (NVL_VatTu.MaHang=NVL_TK.MaChiTiet and NVL_TK.Nhom='" + Nhom1 + "' and NVL_TK.MaSP='"+MaSP1+"')) as qry1 inner join dbo.NVL_VatTu on NVL_VatTu.MaHang=qry1.MaSP";
                }
                else
                    string_temp = @"select NVL_VatTu.TenHang as TenSP,qry1.* from
                            (select NVL_TK.MaSP,NVL_VatTu.TenHang as TenChiTiet, NVL_TK.MaChiTiet,NVL_TK.SLTon,NVL_TK.SLTon/NVL_TK.SLQuyDoi as DBTon, isnull(NVL_TK.ThanhTien,0) as ThanhTien
                            from dbo.NVL_VatTu inner join dbo.NVL_TK on (NVL_VatTu.MaHang=NVL_TK.MaChiTiet and NVL_TK.Nhom='" + Nhom1 + "')) as qry1 inner join dbo.NVL_VatTu on NVL_VatTu.MaHang=qry1.MaSP";
                ds = prs.dts(string_temp, Conn, ds);
                if (radGridView14.Rows.Count > 0)
                {
                    for (i = 0; i < this.radGridView14.ChildRows.Count; i++)
                    {
                        GridViewGroupRowInfo groupRowInfo = this.radGridView14.ChildRows[i] as GridViewGroupRowInfo;
                        groupRowInfo.HeaderText = "";
                    }
                }
                radGridView14.DataSource = ds.Tables[0];
                if (ds.Tables[0].Rows.Count > 0)
                {


                    double min = 0;
                    for (i = 0; i < this.radGridView14.ChildRows.Count; i++)
                    {
                        //this.radGridView4.ChildRows[i].Cells["Serial_KV5Search"].Value = i.ToString();

                        GridViewGroupRowInfo groupRowInfo = this.radGridView14.ChildRows[i] as GridViewGroupRowInfo;
                        if (groupRowInfo != null)
                        {
                            ThanhTien_d = 0;
                            min = double.Parse(radGridView14.ChildRows[i].ChildRows[0].Cells["DBTon_TKNVL"].Value.ToString());
                            //groupRowInfo.HeaderText = groupRowInfo.HeaderText + " " + i.ToString();
                           
                            for (int p = 0; p < this.radGridView14.ChildRows[i].ChildRows.Count; p++)
                            {
                                temp_double = double.Parse(radGridView14.ChildRows[i].ChildRows[p].Cells["DBTon_TKNVL"].Value.ToString());
                               

                                if (min > temp_double)
                                {
                                    min = temp_double;
                                   
                                }
                                ThanhTien_d += double.Parse(radGridView14.ChildRows[i].ChildRows[p].Cells["ThanhTien_TKNVL"].Value.ToString());
                               
                            }
                            //MessageBox.Show(temp_double.ToString());
                            string_temp = groupRowInfo.HeaderText + "       Đồng bộ: " + string.Format("{0:###,###,###,###}", min) + ",        Thành Tiền: " + string.Format("{0:###,###,###,###}", ThanhTien_d);
                            groupRowInfo.HeaderText = string_temp;

                        }

                    }
                }

                else
                    MessageBox.Show("Không có dữ liệu");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
            }
        }
        private void rd_Search_TKNVL_Click(object sender, EventArgs e)
        {
            TonKho_NVL();
        }
        private void bt_excel_TKNVL_Click(object sender, EventArgs e)
        {
            if (radGridView14.Rows.Count > 1)
            {
                DataTable dt = (DataTable)radGridView14.DataSource;
                prs.ExportExcel(dt, "C:\\Huynh\\NVL_TonKho.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }
        #endregion
        #region //Chung Tu Nguyen Vat Lieu
        private void search_NVLCT()
        {
            Conn = prs.Connect();
            SqlCommand cmd = new SqlCommand();
            Conn.Open();
            cmd.Connection = Conn;
            DataSet ds = new DataSet();
            try
            {
                if (txt_SoPhieu_NVLCT.Text != "" && txt_Serial_NVLCT.Text != "")
                    MessageBox.Show("Bạn chỉ nhập 1 trong 2 thông tin trên là được");
                else
                {
                    if (txt_SoPhieu_NVLCT.Text == "" && txt_Serial_NVLCT.Text == "")
                        MessageBox.Show("Bạn cần nhập SỐ PHIẾU hoặc SERIAL ");
                    else
                    {
                        string_temp = @"SELECT [NoiGiao],[NoiNhan],[Nhom],[LyDo],convert(varchar(10),[Ngay],103) as Ngay,[DienGiai],Serial
                                    FROM [dbo].[NVL_ChungTu]";
                        if (txt_Serial_NVLCT.Text != "")
                            string_temp += " where Serial=" + txt_Serial_NVLCT.Text;
                        if (txt_SoPhieu_NVLCT.Text != "")
                            string_temp += " where SoPhieu='" + txt_SoPhieu_NVLCT.Text + "'";
                        cmd.CommandText = string_temp;
                        SqlDataReader rd = cmd.ExecuteReader();
                       
                        while (rd.Read())
                        {
                            NoiGiao1 = rd["NoiGiao"].ToString();
                            NoiNhan1 = rd["NoiNhan"].ToString();
                            LyDo1 = rd["LyDo"].ToString();
                            dtp_Date_NVLCT.Text = rd["Ngay"].ToString();
                            txt_DienGiai_NVLCT.Text = rd["DienGiai"].ToString();
                            Serial1 = rd["Serial"].ToString();
                        }
                        //MessageBox.Show("aaaa");
                        for (i = 0; i < cb_NoiGiao_NVLCT.Items.Count; i++)
                        {
                            //MessageBox.Show(cb_NoiGiao_NVLCT.Items[i].ToString());
                            if (NoiGiao1 == prs.substring_left(cb_NoiGiao_NVLCT.Items[i].ToString(), child))
                                NoiGiao1 = cb_NoiGiao_NVLCT.Items[i].ToString();
                            if(NoiNhan1==prs.substring_left(cb_NoiGiao_NVLCT.Items[i].ToString(),child))
                                NoiNhan1=cb_NoiGiao_NVLCT.Items[i].ToString();
                        }
                        for (i = 0; i < cb_LyDo_NVLCT.Items.Count; i++)
                        {
                            if (LyDo1 == prs.substring_right(cb_LyDo_NVLCT.Items[i].ToString(), child))
                            {
                                LyDo1 = cb_LyDo_NVLCT.Items[i].ToString();
                                break;
                            }
                            

                        }
                        cb_NoiGiao_NVLCT.Text=NoiGiao1;
                        cb_NoiNhan_NVLCT.Text=NoiNhan1;
                        cb_LyDo_NVLCT.Text = LyDo1;
                        txt_Serial_NVLCT.Text = Serial1;
                        txt_Serial_NVLCT.ReadOnly = true;
                        //rd.Close();
                        rd.Dispose();
                        string_temp = @"SELECT  NVL_NhapXuat.[Serial],dbo.NVL_ChungTu.SoPhieu,NVL_NhapXuat.ChungTu,dbo.NVL_ChungTu.NoiGiao,dbo.NVL_ChungTu.NoiNhan,dbo.NVL_ChungTu.Ngay,dbo.NVL_ChungTu.LyDo,dbo.NVL_ChungTu.DienGiai
                                  ,NVL_VatTu.[MaHang],TenHang,dbo.NVL_ChungTu.Nhom
                                  ,(SLNhap+SLXuat) as SoLuong
                                  ,NVL_NhapXuat.[DonGia]
                                   ,(SLNhap+SLXuat)*NVL_NhapXuat.DonGia as ThanhTien
                                  ,NVL_NhapXuat.[DVT]
                                  ,[GhiChu]
                              FROM [dbo].[NVL_NhapXuat] inner join dbo.NVL_ChungTu on NVL_NhapXuat.ChungTu=NVL_ChungTu.Serial inner join dbo.NVL_VatTu on NVL_VatTu.MaHang=NVL_NhapXuat.MaHang where NVL_NhapXuat.ChungTu = " + Serial1;
                        ds = prs.dts(string_temp, Conn, ds);
                        if (ds.Tables[0].Rows.Count > 0)
                        {
                            SoLuong_d = 0;
                            ThanhTien_d = 0;
                            radGridView15.DataSource = ds.Tables[0];
                            for (i = 0; i < ds.Tables[0].Rows.Count; i++)
                            {
                                SoLuong_d += double.Parse(ds.Tables[0].Rows[i]["SoLuong"].ToString());
                                ThanhTien_d += double.Parse(ds.Tables[0].Rows[i]["ThanhTien"].ToString());

                            }
                            lb_Tong_NVLCT.Text = string.Format("{0:###,###.##}", SoLuong_d);
                            lb_TongTien_NVLCT.Text = string.Format("{0:###,###,###,###}", ThanhTien_d);

                        }
                        else
                            MessageBox.Show("Không có dữ liệu");

                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                
            }
        }
        private void bt_search_NVLCT_Click(object sender, EventArgs e)
        {
            search_NVLCT();
        }
        private void edit_NVLCT()
        {
            if (cb_NoiGiao_NVLCT.Text == "" || cb_NoiNhan_NVLCT.Text == "" || cb_LyDo_NVLCT.Text == "")
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin");
            else
            {
                NoiGiao1 = prs.substring_left(cb_NoiGiao_NVLCT.Text, child);
                NoiNhan1 = prs.substring_left(cb_NoiNhan_NVLCT.Text, child);
                LyDo1 = prs.substring_right(cb_LyDo_NVLCT.Text, child);
                GhiChu1 = txt_DienGiai_NVLCT.Text;
                string[] temp = dtp_Date_NVLCT.Text.Split('/');
                Ngay1 = temp[1] + "/" + temp[0] + "/" + temp[2];
                if (NoiGiao1 == "" || NoiNhan1 == "" || LyDo1 == "")
                    MessageBox.Show("Kiểm tra lại thông tin nhập");
                else
                {
                    DialogResult TL;
                    TL = MessageBox.Show("BẠN CÓ MUỐN SỬA SERIAL " + txt_Serial_NVLCT.Text + " HOẶC SỐ PHIẾU "+txt_SoPhieu_NVLCT.Text+" KHÔNG ?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                    if (TL == DialogResult.Yes)
                    {
                        Conn = prs.Connect();
                        SqlCommand cmd = new SqlCommand();
                        Conn.Open();
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Connection = Conn;
                        //cmd.CommandText = "";
                        try
                        {

                            cmd.CommandText = "Upd_NVL_ChungTu";
                            cmd.Parameters.AddWithValue("@Serial", txt_Serial_NVLCT.Text);
                            //cmd.Parameters.AddWithValue("@SoPhieu", txt_SoPhieu_inputNVL.Text.Trim());
                            cmd.Parameters.AddWithValue("@NoiGiao", NoiGiao1);
                            cmd.Parameters.AddWithValue("@NoiNhan", NoiNhan1);
                            //cmd.Parameters.AddWithValue("@Nhom", Nhom1);
                            cmd.Parameters.AddWithValue("@LyDo", LyDo1);
                            cmd.Parameters.AddWithValue("@Ngay", Ngay1);
                            cmd.Parameters.AddWithValue("@DienGiai", GhiChu1);
                           
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("Dữ liệu Chỉnh sửa thành công");
                            bt_reset_NVLCT_Click(null, null);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                        finally
                        {
                            Conn.Close();
                            cmd.Dispose();
                        }
                    }
                }
            }
        }
        private void bt_edit_NVLCT_Click(object sender, EventArgs e)
        {
            edit_NVLCT();
        }
        private void bt_reset_NVLCT_Click(object sender, EventArgs e)
        {
            cb_NoiGiao_NVLCT.Text = "";
            cb_NoiNhan_NVLCT.Text = "";
            cb_LyDo_NVLCT.Text = "";
            txt_DienGiai_NVLCT.Text = "";
            txt_Serial_NVLCT.Text = "";
            txt_Serial_NVLCT.ReadOnly = false;
            txt_SoPhieu_NVLCT.Focus();
            for (i = radGridView15.Rows.Count - 1; i >= 0; i--)
            {
                radGridView15.Rows.RemoveAt(i);
            }
        }
        #endregion
        #region //Report Nguyen Vat Lieu
        private void report_NVLTH()
        {
            DataSet ds = new DataSet();
            Conn = prs.Connect();
            Conn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = Conn;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "prc_NVL_TH";
            try
            {
                Datebegin = Date_Split(dtp_DateBegin_NVLRP.Text);
                Dateend = Date_Split(dtp_DateEnd_NVLRP.Text);
                Datebegin = Datebegin + " 00:00";
                Dateend = Dateend + " 23:59";
                if (rd_BaoBi_NVLRP.IsChecked == true)
                {
                    Nhom1 = "Nhóm: Bao Bì";
                    string_temp = "BaoBi";
                }
                if (rd_DauMau_NVLRP.IsChecked == true)
                {
                    Nhom1 = "Nhóm: Dầu Màu";
                    string_temp = "DauMau";
                }
                if (rd_Fitting_NVLRP.IsChecked == true)
                {
                    Nhom1 = "Nhóm: Fitting";
                    string_temp = "Fitting";
                }
                cmd.Parameters.AddWithValue("@datebegin", Datebegin);
                cmd.Parameters.AddWithValue("@dateend", Dateend);
                cmd.Parameters.AddWithValue("@Nhom", string_temp);
                if (cb_TenSP_NVLRP.Text != "")
                {
                    MaSP1 = prs.substring_left(cb_TenSP_NVLRP.Text, child);
                    cmd.Parameters.AddWithValue("@check", "1");
                    cmd.Parameters.AddWithValue("@MaHang", MaSP1);
                }
                else
                {
                    cmd.Parameters.AddWithValue("@check", "0");
                    cmd.Parameters.AddWithValue("@MaHang", "0");
                }
                //MessageBox.Show(Datebegin + " " + Dateend + " " + string_temp+" ");
                ds = prs.dts_sql(cmd, Conn, ds);
                if (ds.Tables[0].Rows.Count > 0)
                {

                    Fr_Report FrR = new Fr_Report();
                    string_temp = dtp_DateBegin_NVLRP.Text + " - " + dtp_DateEnd_NVLRP.Text;
                    GhiChu1 = lb_HovaTen.Text;
                    
                    FrR.report_NVL_TH(ds, string_temp, Nhom1, GhiChu1);
                    // FrR.report_KV1NKTG(ds, Ngay1);
                    FrR.Show();

                }
                else
                {
                    MessageBox.Show("Không có dữ liệu. Vui lòng kiểm tra lại thông tin tìm kiếm");
                }

                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();
                cmd.Dispose();
                 ds.Clear();
            }
        }
        private void bt_print_NVLRP_Click(object sender, EventArgs e)
        {
            report_NVLTH();
        }
        #endregion
        #region //Bang Ke Nguyen Vat Lieu
        private void BangKe_NVL()
        {
            Conn = prs.Connect();
            Conn.Open();
            DataSet ds = new DataSet();
            Datebegin = Date_Split(dtp_Date_NVLBK.Text);
            try
            {
                string_temp = @"SELECT [Serial],NVL_VatTu.[MaHang],NVL_VatTu.TenHang,[SLNhap],[SLXuat],[NVL_NhapXuat_CT].[DonGia],(SLNhap+SLXuat)*[NVL_NhapXuat_CT].DonGia as ThanhTien,[SoPhieu]
            ,[NoiGiao],[NoiNhan],[NVL_NhapXuat_CT].[Nhom],[LyDo],convert(varchar(10),[Ngay],103) as Ngay,[DienGiai],[Serial_NX],[NVL_NhapXuat_CT].[DVT]
            ,[GhiChu],[UserNhap],[UserSua],[NgayInsert]
            FROM [dbo].[NVL_NhapXuat_CT] inner join dbo.NVL_VatTu on NVL_VatTu.MaHang=NVL_NhapXuat_CT.MaHang where NgayInsert>='" + Datebegin+" 00:00' and NgayInsert<='"+Datebegin+" 23:59' order by Serial_NX";
                ds = prs.dts(string_temp, Conn, ds);
                //MessageBox.Show(string_temp);
                radGridView16.DataSource = ds.Tables[0];
                if (ds.Tables[0].Rows.Count == 0)
                    MessageBox.Show("Không có dữ liệu. Vui lòng kiểm tra thong tin tìm kiếm");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                Conn.Close();

            }
        }
        #endregion

        private void bt_Search_NVLBK_Click(object sender, EventArgs e)
        {
            BangKe_NVL();
        }

        private void radGridView2_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView2);
        }

        private void radGridView7_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView7);
        }

        private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            prs.Formatgridview(sender, e, radGridView1);

        }

        private void bt_exportexcel_ralo_Click(object sender, EventArgs e)
        {
            if (radGridView9.Rows.Count > 0)
            {
                DataTable dt = (DataTable)radGridView9.DataSource;
                prs.ExportExcel_grid(radGridView9, "C:\\Huynh\\LosayRalo.xlsx", 6, 1);
            }
            else
                MessageBox.Show("Không có dữ liệu để xuất ra excel");
        }

      

       

       

        

       

       

       
    }
}
